One, Nginx load balancing algorithm
1. Polling (default)
Each request is assigned to a different backend service in chronological order, and if a server in the back end freezes, the failed system is automatically rejected, so that user access is not affected.
2. Weight (polling weights)
The larger the value of the weight, the higher the access probability assigned to it, which is mainly used for the performance imbalance of each server in the backend. Or simply to set different weights in the case of the master and slave, to achieve a reasonable and efficient use of host resources.
3, Ip_hash
Each request is allocated according to the hash result of the access IP, so that visitors from the same IP have fixed access to a back-end server, and can effectively solve the session sharing problem of dynamic Web pages.
4, fair
A more intelligent load balancing algorithm than weight and Ip_hash, the fair algorithm can intelligently load balance based on the size of the page and the length of time it takes to load, that is, to allocate requests based on the response time of the backend server, with short response times. Nginx itself does not support fair, and if this scheduling algorithm is required, the Upstream_fair module must be installed.
5, Url_hash
The efficiency of the back-end cache server can be further improved by assigning requests to the hash results of the URLs accessed, and directing each URL to a back-end server. Nginx itself does not support url_hash, if you need this scheduling algorithm, you must install Nginx hash package.
Second, Nginx load balancing scheduling state
In the Nginx upstream module, you can set the state of each back-end server in load balancing scheduling, and the commonly used states are:
1, down, indicates that the current server is temporarily not participating in load balancing
2, Backup, reserved machine. When all other non-backup machines fail or are busy, the backup machine is requested, so the machine has the lowest access pressure
3, Max_fails, the number of times the request failed to be allowed, the default is 1, and when the maximum number of times is exceeded, the error defined by the Proxy_next_upstream module is returned.
4, Fail_timeout, request failure time-out, after the max_fails failed, the time to pause the service. Max_fails and fail_timeout can be used together.
Three, Nginx load Balancing configuration
Add the upstream module to the HTTP layer of the Nginx configuration file
http {upstream www.huangming.org { server 192.168.1.209:80 weight=2 max_fails=3 fail_timeout=10s; server 192.168.1.250:80 weight=1 max_fails=3 fail_timeout=10s;} server { listen 80; server_name www.huangming.org 192.168.1.21; index index.html index.htm index.php index.jsp; location / { proxy_pass http://www.huangming.org; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote _addr; proxy_set_header X-Forwarded-For $proxy _add_x_forwarded_for; proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; }}
[[email protected] ~]# Curl Www.huangming.orgweb2[[email protected] ~]# Curl Www.huangming.orgweb2[[email protected] ~]# Curl Www.huangming.orgweb1[[email protected] ~]# Curl WWW.HUANGMING.ORGWEB2
The two realserver in the example above are:
192.168.1.209
192.168.1.250
Both Realserver installed Nginx as the backend Web server
The Nignx load balancing function is implemented through the upstream module and is a 7-layer switching load balancer based on content and applications. Nginx Load Balancer default to the back-end server has health detection capability, but the detection capacity is weak, limited to port detection, in the back-end server less (10 and below) load balancing ability performance. Compared with LVS load balancer, LVS is a four-layer IP load balancing technology, which has the advantages of high performance, high availability and large throughput, and LVS is better in the cluster.
Nginx load Balancing (i)