Nginx can perform load balancing according to the client IP. By setting ip_hash in the upstream, the same backend server can be selected for the client in the same class C address segment, unless the backend server is down.
5 ways of distribution currently supported by nginx's upstream
1. Polling (default)
Each request is allocated to different back-end servers one by one in chronological order. If the back-end server is down, it can be automatically eliminated.
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2. Specify the weight to
specify the polling probability. The weight is proportional to the access ratio, which is used when the performance of the backend server is uneven.
upstream backserver {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3. IP binding ip_hash
Each request is allocated according to the hash result of the access ip, so that each visitor has a fixed access to a back-end server, which can be solved session problem.
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4. Fair (third party)
allocates requests according to the response time of the back-end server, and priority is given to those with short response time.
upstream backserver {
server server1;
server server2;
fair;
}
5. url_hash (third party)
allocates requests according to the hash result of accessing the url, so that each url is directed to the same back-end server, which is more effective when the back-end server is cached.
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
Add proxy_pass to the server that needs to use load balancing
http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down ; (down means that the server before the order does not participate in the load temporarily)
server 127.0.0.1:8080 weight=2; (weight defaults to 1. The greater the weight, the greater the weight of the load)
server 127.0.0.1:6060;
server 127.0. 0.1:7070 backup; (when all other non-backup machines are down or busy, request the backup machine)
}
max_fails : The default number of requests allowed to fail is 1. When the maximum number of times is exceeded, the error defined by the proxy_next_upstream module is returned
fail_timeout: pause time after max_fails failures