Due to the development of the company's business, a single server has been unable to meet the concurrency and user needs, so only through the horizontal expansion of the way to solve the machine, the line is Nginx+tomcat cluster to solve the way. Because the current business volume is not very large, and because previous code problems require that the same request necessarily map to a specific server to process the request. So Nginx's load balancing strategy chooses Ip_hash.
1.ip_hasp Policy Description
Nginx upstream by default is a poll-based load balancing, in this way, each request in chronological order to a different back-end server, if the backend server down, can be automatically rejected.
Another way is Ip_hash: Each request is allocated according to the hash result of the access IP, so that each visitor fixed access to a back-end server can solve the session problem.
2. First attempt
Upstream Backend {
Ip_hash;
Server 192.168.1.251;
Server 192.168.1.252;
Server 192.168.1.247;
}
Location/{
#禁用缓存
Proxy_buffering off;
#反向代理的地址
Proxy_pass Http://backend;
}
I based on the Nginx official website configuration file to achieve, but this does not work, unable to locate a server, resulting in a request exception.
3. The second attempt
Set the host header and the client real address so that the server gets the client real IP, so I configure
Location/{ #设置主机头和客户端真实地址 so that the server obtains the client's real IP 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_buffering off; #反向代理的地址 proxy_pass http://backend; }
Incredibly can, in fact, I have not seen nginx source implementation, do not know why, but through the enactment of the IP address can let it get to the client real IP address, and then through the load balancing strategy to solve the problem.
4. Attach the full nginx configuration file
#运行用户user Www-data; #启动进程, usually set to equal to the number of CPUs worker_processes 1; #全局错误日志及PID文件error_log/var/log/nginx/error.log;pid/var/run/nginx.pid; #工作 Maximum number of modes and connections events {use Epoll; #epoll是多路复用IO (I/O multiplexing) in one way, but only for linux2.6 above the core, can greatly improve the performance of Nginx worker_connections 1024x768; #单个后台worker Maximum number of concurrent links for process processes # multi_accept on; #设定http服务器, using its reverse proxy function to provide load Balancer support for HTTP {#设定mime类型, the type is defined by the Mime.type file include/etc/nginx/mime.types; Default_type Application/octet-stream; #设定日志格式 Access_log/var/log/nginx/access.log; #sendfile instruction Specifies whether Nginx calls the Sendfile function (zero copy mode) to output files, for normal applications, #必须设为 on, if used for downloading applications such as disk IO heavy-duty applications, can be set to off to balance disk and network I/O processing speed and reduce the uptime of the system. Sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; Keepalive_timeout 65; Tcp_nodelay on; #开启gzip压缩 gzip on; Gzip_disable "MSIE [1-6]\. (?!. *SV1) "; #设定请求缓冲 Client_header_buffer_size 1k; Large_client_header_buffers 4 4k; Include/etc/nginx/conf.d/*.conf; include/etc/nginx/sites-enabled/*; #设定负载均衡的服务器列表 upstream Mysvr {#weigth参数表示权值, the higher the weight, the greater the chance of being allocated #本机上的Squid开启3128端口 server 192.168.8.1:3128 weight= 5; Server 192.168.8.2:80 weight=1; Server 192.168.8.3:80 weight=6; } server {#侦听80端口 listen 80; #定义使用www. xx.com access to server_name www.xx.com; #设定本虚拟主机的访问日志 Access_log Logs/www.xx.com.access.log Main; #默认请求 location/{root/root; #定义服务器的默认网站根目录位置 index index.php index.html index.htm; #定义首页索引文件的名称 Proxy_pass http://mysvr; #请求转向mysvr the server list #以下是一些反向代理的配置可删除 defined. Proxy_redirect off; #后端的Web服务器可以通过X-forwarded-for Get the user real IP 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_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数, Proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间 (proxy connection timeout) Proxy_send_timeout 90; #后端服务器数据回传时间 (proxy send timeout) proxy_read_timeout 90; #连接成功后, back-end server response time (proxy receive timeout) Proxy_buffer_size 4k; #设置代理服务器 (Nginx) Save the user header information buffer size Proxy_buffers 4 32k; #proxy_buffers缓冲区, the average page below 32k, so set proxy_busy_buffers_size 64k; #高负荷下缓冲大小 (proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小, greater than this value, the # definition error page will be passed from the upstream server Error_page 502 503 504/50x.html; Location =/50x.html {root/root; } #静态文件, Nginx processing location ~ ^/(images|javascript|js|css|flash|media|static)/{root/var/www/virtual/htdocs; #过期30天, the static files are not updated, the expiration can be set a little larger, if updated frequently, you can set a smaller point. Expires 30d; #PHP script requests are forwarded to fastcgi processing. Use the fastcgi default configuration. Location ~ \.php$ {root/root; Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Fastcgi_param script_filename/home/www/www$fAstcgi_script_name; Include Fastcgi_params; } #设定查看Nginx状态的地址 location/nginxstatus {stub_status on; Access_log on; Auth_basic "Nginxstatus"; Auth_basic_user_file conf/htpasswd; #禁止访问. htxxx file Location-/\.ht {deny all; } }}5. Summary
Gig is not the best way to solve the problem, be prepared is the policy, look back and study, this reason is why.
Nginx Balanced Load (ip_hash) not in effect