Nginx load balancing configuration instructions

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
The allocation of nginx upstream currently supports 4 modes
1), Polling (default)
Each request according to the time sequence are assigned to different backend server, if the server down off, can automatically remove.
2), weight
Specify the polling probability, weight and access rate is proportional to the back-end server performance, uneven, the default is 1.
2), ip_hash
Each request to access the IP hash results distribution, so that each visitor fixed access a back-end server, session can solve the problem of.
3), Fair (third party)
According to the response time of the back-end server to assign priority allocation request, response time is short.

4), Url_hash (third): according to the URL hash results to the allocation request, so that each URL directed to the same backend server

Add the HTTP node.:
#The definition of load balancing device of Ip and equipment state
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
#Add in the Server node using the load under
proxy_pass http://myServer;
#A server can be configured with multiple equilibria, air spaced

The upstream status of each device:
Down represents a single server before temporarily not participating in the load
Weight default is 1.weight larger load, the more weight.
max_fails : Number of allowed requests failed 1 by default when more than the maximum number, return to the proxy_next_upstream module defined error
Fail_timeout:max_fails failed, pause time.
backup: All other non backup machine down or busy time, request the backup machine. So this machine will be the lightest pressure.


Nginx also supports load balancing of multi group, you can configure multiple upstream services in different Server.
Configure load balancing is relatively simple, the question is how to realize the sharing but between session servers
There are many ways (the following content from the Internet, fourth methods are not practical.)
1) Do not use session, for cookie
If the program logic is not complex, the session into cookie
2) The application server to realize the sharing of
Can use a database or memcached to save session, its efficiency is not high, not suitable for the requirements for high efficiency.
3) ip_hash
Nginx ip_hash technology can be a IP requests to the same back-end, so the IP a client and a back-end can build a solid session, ip_hash is defined in the upstream configuration.:
upstream backend {
server 127.0.0.1:8080 ;
server 127.0.0.1:9090 ;
ip_hash;
}
Ip_hash is easy to understand, but can only use the IP factor to assign back, so the ip_hash is defective, cannot be used in some cases:
1, Nginx is not the most front-end server. Ip_hash nginx is the most front-end server, or nginx not correct according to the IP IP, not hash. For example, using squid for the front, then nginx and IP will get a server IP address squid, the address as shunt is sure.


2, Load balancing nginx backend and other means. If the nginx backend and other load balancing, the request and through other means billabong, then the request cannot locate a client to a session application server. In that case, nginx is only pointing directly to the application server, or by a squid, and then point to the application server. The best way is to use location as a diversion, will need to be part of the session request through the ip_hash shunt, the rest go back to the other.



client_max_body_size 300m; //The number of single file byte maximum allowed the client request.
client_body_buffer_size 128k; //Cache proxy client file bytes
client_body_temp_path /dev/shm/client_body_temp; //Request to write to the cache file directory path
proxy_connect_timeout600; //And the back-end server connection timeout,
proxy_read_timeout 600; //A successful connection for back-end corresponding time, the default is 60S
proxy_send_timeout 600; //Reverberation time back-end servers, provision server in a certain period of time after transmitting.
proxy_buffer_size 16k; //The proxy server cache file head file size, the default is 4K
proxy_buffers 4 32k; //The back-end is really the number and size of server
proxy_busy_buffers_size 64k; //When the system is busy, for larger proxy_buffer
proxy_temp_file_write_size 64k; //Write to the temporary directory file size
proxy_temp_path /dev/shm/proxy_temp; //The request specifies a directory to the cache is large

proxy_pass http://cluster/; //Need to specify the proxy URL,
proxy_redirect off; //If you need to open the location and Refresh fields from the rear, can open. That is to say the back-end and the proxy server, need to open
proxy_set_header X-Real-IP $remote_addr; //Permits will be sent to the backend server requests a re definition or add a field, this can be a variable is text combination.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //Written in PS with the following, $http_x_Forwarded_for this even add in back-end Web, There was no user IP, So in the nginxThe reverse proxy to addHeaderHeader information X-Forwarded-ForIn conjunction with the back-end server log file$http_x_Forwarded_forThis can get the userIPAddress.
proxy_set_header Host $host; //The first proxy_set_header directives Header header information specified in the request to the reverse proxy server back-end Web, The back-end web server has multiple virtual host domain name based, The head information of Host, Used to specify a name for, So the back-end web can identify the reverse proxy request which virtual host processing.
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503
http_504 http_404; Various state server timeout corresponding head



Example:

upstream cluster {
server 192.168.100.238:80 weight=8 max_fails=2 fail_timeout=30s;
server 192.168.100.248:80 weight=8 max_fails=2 fail_timeout=30s;
}


server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Yolanda at January 16, 2014 - 3:13 PM