Five load balancing nginx configuration methods are Shared

  • 2020-05-10 23:15:41
  • OfStack

1. Polling (default)  

Each request is assigned to a different backend server 1 by 1 in chronological order. If the backend server down is dropped, it can be automatically culled.  

2. weight

Specify the polling probability that weight is proportional to the access ratio for situations where the backend server performance is uneven.  
For example:  

upstream bakend {  
server 192.168.0.14 weight=10; 
server 192.168.0.15 weight=10; 
}

3. ip_hash

Each request is allocated according to the hash result of accessing ip, so that each visitor has a fixed access to one back-end server, which can solve the problem of session.  
For example:  

upstream bakend {  
ip_hash; 
server 192.168.0.14:88; 
server 192.168.0.15:80; 

4. fair (third party)

Requests are allocated according to the response time of the back-end server, and priority is given to those with short response times.  

upstream backend {  
server server1; 
server server2; 
fair; 
}
 

5. url_hash (third party)    

Requests are allocated according to the hash result of accessing url, so that each url is directed to the same back-end server, which is more efficient when the back-end server is cached.  
Example: add hash statement in upstream, server statement cannot write weight and other parameters, hash_method is the hash algorithm   used

upstream backend {  
server squid1:3128; 
server squid2:3128; 
hash $request_uri; 
hash_method crc32; 
}
 
tips:  
upstream bakend{# Of a load balancing device Ip And equipment status   
ip_hash; 
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   to server where load balancing is required

proxy_pass http://bakend/;
 

The state of each device is set to:  

1.down means that server before the order is temporarily not involved in the load  
2.weight defaults to 1.weight. The larger the weight, the greater the load weight.  
3.max_fails: the default number of failed requests allowed is 1. When the maximum number is exceeded, proxy_next_upstream module definition error   is returned
4.fail_timeout: the time to pause after max_fails failed a number of times.  
5.backup: all other non-backup machines down or request backup machines when busy. So this machine will have the least pressure.  
nginx supports multi-group load balancing at the same time for use by unused server.  
client_body_in_file_only is set to On. You can record the data from client post into the file to do debug  
The directory of the client_body_temp_path Settings record file can be set to up to three levels of the   directory
location matches URL. Redirection or new proxy load balancing is possible


Related articles: