Five nginx load balancing configuration methods are Shared

  • 2020-05-07 20:55:41
  • OfStack

Polling (default)  

Each request is allocated to a different backend server one by one in chronological order, and can be automatically culled if the backend server down is dropped.  

2. weight

Specifies the polling probability, weight being proportional to the access ratio, for use in cases of uneven backend server performance.  
For example:  

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

3. ip_hash

Each request is assigned according to the hash result of accessing ip, so that each visitor has fixed access to 1 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 the short response time.  

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 one 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 used hash algorithm  

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  
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, the error   defined by the proxy_next_upstream module is returned
4.fail_timeout: time to pause after max_fails failed.  
5.backup: all other non-backup machines down or when busy, request backup machines. So this machine will have the least pressure.  
nginx supports multi-group load balancing at the same time for use by different server.  
client_body_in_file_only set to On can record the data from client post to the file for 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: