Nginx upstream is Shared in 5 ways of weight assignment

  • 2020-05-06 12:06:01
  • OfStack

Polling (default)

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

2, weight
Specifies the polling probability, weight proportional to the access ratio, for situations where the backend server performance is uneven.
For example:
 
upstream backend { 
server 192.168.0.14 weight=10; 
server 192.168.0.15 weight=10; 
} 

3, ip_hash
resolves the session problem by assigning each request to the hash result of accessing ip so that each visitor has fixed access to one back-end server.
For example:
 
upstream backend { 
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.linuxany.com; 
server server2.linuxany.com; 
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
 
upstream backend { 
server squid1:3128; 
server squid2:3128; 
hash $request_uri; 
hash_method crc32; 
} 
# Of a load balancing device Ip And equipment status  
upstream backend{ 
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 participating 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, an proxy_next_upstream module definition error
is returned 4. Time to pause after fail_timeout:max_fails failed a number of times.
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, which is used by unused 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: