Nginx can load balance according to client IP, set ip_hash in upstream, can select the same back-end server for clients in the same Class C address segment, unless that backend server is down.
Nginx's upstream currently supports 5 ways to allocate
1. Polling (default)
each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.
upstream Backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2. Assigning Weights
Specifies the polling probability, proportional to the weight and access ratios, for situations where 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 fixed access to a back-end server that resolves the session issue.
upstream Backserver {
Ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4. Fair (third party)
The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.
upstream Backserver {
server server1;
server server2;
Fair;
}
5. Url_hash (third party)
assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.
upstream Backserver {
server squid1:3128;
server squid2:3128;
hash $request _uri;
Hash_method CRC32;
}
in servers that need to use load balancing, add
Proxy_pass http://backserver/;
Upstream backserver{
Ip_hash;
server 127.0.0.1:9090 down; (down indicates that the server is temporarily not participating in the load)
server 127.0.0.1:8080 weight=2; (weight defaults to 1.weight, the greater the load weight)
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 number of times that a request failed is allowed defaults to 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded
Fail_timeout:max_fails times after a failure, the time of the pause
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nginx Load balancer based on Ip_hash's session sticky posts