[Nginx] - 负载均衡配置

Posted [横渡]

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Nginx] - 负载均衡配置相关的知识,希望对你有一定的参考价值。

nginx.conf文件内容:

user  www www;
worker_processes  2;

error_log  /usr/local/nginx/logs/serror.log crit;
pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 65535;

events 
{
  use epoll;
  worker_connections 65535;
}


http {
	include       mime.types;
	default_type  application/octet-stream;

	#charset  gb2312;

	server_names_hash_bucket_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	client_max_body_size 8m;

	sendfile on;
	tcp_nopush     on;

	keepalive_timeout 60;

	tcp_nodelay on;

	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffers_size 128k;
	fastcgi_temp_file_write_size 128k;

	gzip on;
	gzip_min_length  1k;
	gzip_buffers     4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types       text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	#limit_zone  crawler  $binary_remote_addr  10m;
	log_format ‘$remote_addr - $remote_user [$time_local] "$request" ‘
	              ‘$status $body_bytes_sent "$http_referer" ‘
	              ‘"$http_user_agent" "$http_x_forwarded_for"‘;
	include /usr/local/nginx/conf/vhosts/*.conf;
}

 

在vhosts目录下的conf文件内容:

upstream slave {
	server xxx.xxx.xxx.xxx max_fails=3 fail_timeout=30s weight=5;
	server xxx.xxx.xxx.xxx max_fails=3 fail_timeout=30s weight=5;
}

server {
	listen	80;
	server_name  www.XXX.com;
	
	#if ($uri ~* "(/login$|/login.html$|/login/index.html$|/admin/.+|/signup$|/signup/.+$|/signup.html$)") {
	#	return	301	https://$server_name$request_uri;
	#}

	location / {
		client_max_body_size 200m;
		proxy_pass http://slave;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		client_body_buffer_size 128k;
		proxy_connect_timeout 600;
		proxy_read_timeout 600;
		proxy_send_timeout 600;
		proxy_buffer_size 64k;
		proxy_buffers 4 32k;
		proxy_busy_buffers_size 64k;
		proxy_temp_file_write_size 64k;
	}

	location = /50x.html {
		root   html;
	}
}

server{
	listen 443;
	ssl on;
	ssl_certificate /alidata/ssl/server.crt;
	ssl_certificate_key /alidata/ssl/server.key;
	server_name  www.XXX.com;
	
	#if ($uri !~* "(/login$|/login.html$|/login/index.html$|/static/.+|/common/api/.+|/admin/.+|/signup$|/signup/.+$|/signup.html$)") {
	#	return	301	http://$server_name$request_uri;
	#}

	location / {
		client_max_body_size 200m;
		proxy_pass http://slave;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		client_body_buffer_size 128k;
		proxy_connect_timeout 600;
		proxy_read_timeout 600;
		proxy_send_timeout 600;
		proxy_buffer_size 64k;
		proxy_buffers 4 32k;
		proxy_busy_buffers_size 64k;
		proxy_temp_file_write_size 64k;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_redirect off;
	}

	location = /50x.html {
		root   html;
	}
}

  

以上是关于[Nginx] - 负载均衡配置的主要内容,如果未能解决你的问题,请参考以下文章

nginx 配置负载均衡服务器

使用Nginx配置TCP负载均衡

Nginx负载均衡配置

nginx 负载均衡配置

OpenResty 动态负载均衡

Nginx实现负载均衡