ubuntu nginx配置负载均衡篇(二)

这里提供部分我的配置文件:

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
	
	upstream mysvr {
		#weigth参数表示权值,权值越高被分配到的几率越大
		#本机上的Squid开启3128端口
		server 192.168.0.1:80  weight=1;
        server 192.168.0.2:80  weight=1;
		server 192.168.0.3:80  weight=1 backup;
	}
	upstream mysvr1 {
     		server 192.168.0.5:80; 
    	}
}

/etc/nginx/sites-enabled/default: 我使用的是监听本地5000端口

server {
	listen 5000 default_server;
	listen [::]:5000 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/nginx;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		ssi on;
		ssi_silent_errors on;
		# 读取最大时间
		proxy_read_timeout 300;
		proxy_buffer_size 4k;
		proxy_buffers 4 32k;
		proxy_busy_buffers_size 64k;
		proxy_temp_file_write_size 64k;
		# 连接代理服务超时时间
		proxy_connect_timeout 90; 
		# 请求发送最大时间
		proxy_send_timeout 90;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://mysvr;
	}
	
	location /mysvr1/{
		ssi on;
		ssi_silent_errors on;
		# 读取最大时间
		proxy_read_timeout 300;
		proxy_buffer_size 4k;
		proxy_buffers 4 32k;
		proxy_busy_buffers_size 64k;
		proxy_temp_file_write_size 64k;
		# 连接代理服务超时时间
		proxy_connect_timeout 90;
		# 请求发送最大时间
		proxy_send_timeout 90;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://mysvr1;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

这样比如我访问 192.168.0.1:5000端口, 我会根据负载均衡策略分别转发到

server 192.168.0.1:80  和server 192.168.0.2:80  ,一旦两个服务都挂掉了,会使用备份服务器server 192.168.0.3

发布了145 篇原创文章 · 获赞 120 · 访问量 163万+

猜你喜欢

转载自blog.csdn.net/pbymw8iwm/article/details/105416245
今日推荐