Home >Backend Development >PHP Tutorial >Sharing of 5 weight allocation methods of Nginx upstream
1. Polling (default)
Each request is assigned to a different backend server one by one in chronological order. If the backend server goes down, it can be automatically eliminated.
2. Weight
Specifies the polling probability, weight is proportional to the access ratio, and is used when the performance of the back-end server is uneven.
For example:
Copy the code as follows:
upstream backend {
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 the accessed ip, like this Each visitor has fixed access to a backend server, which can solve session problems.
For example:
Copy the code as follows:
upstream backend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4. fair (third party)
According to the response time of the backend server To allocate requests, priority will be given to those with short response times.
Copy the code as follows:
upstream backend {
server server1.linuxany.com;
server server2.linuxany.com;
fair;
}
5. url_hash (third party)
Distribute requests according to the hash result of the accessed URL. Direct each URL to the same backend server. It is more effective when the backend server is cached.
Example: Add a hash statement to upstream. Other parameters such as weight cannot be written in the server statement. hash_method is the hash algorithm used.
Copy the code as follows:
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
#Define the IP and device status of the load balancing device
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;
}
In the server that needs to use load balancing, add
proxy_pass http://bakend/;
The status of each device is set to:
1.down Indicates that the previous server will not participate in the load for the time being
2.weight The default is 1. The larger the weight, the greater the weight of the load.
3.max_fails: The number of allowed request failures defaults to 1. When the maximum number is exceeded, the error defined by the proxy_next_upstream module is returned.
4.fail_timeout: The pause time after max_fails failures.
5.backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the least pressure.
nginx supports setting up multiple groups of load balancing at the same time for use by unused servers.
client_body_in_file_only is set to On, and the data from the client post can be recorded into a file for debugging.
client_body_temp_path can set up to 3 levels of directories by setting the directory of the recording file.
location matches the URL. You can redirect or perform a new proxy load. balanced
The above has introduced the sharing of 5 weight allocation methods of Nginx upstream, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.