Nginx implements load balancing in this way
Generally, multi-server load splitting is used to solve server load problems. Common solutions include:
- The website portal is connected to the server load through sub-stations (such as the sky software station and huajun Software Park)
- DNS round robin
- F5 physical device
- Nginx and Other lightweight Architectures
Let's take a look at how Nginx achieves load balancing. Nginx upstream currently supports the following methods of allocation:
- Round Robin (default) each request is distributed to different backend servers one by one in chronological order. If the backend server is down, it can be automatically removed.
- Weight specifies the round-robin probability. weight is proportional to the access ratio, which is used when the backend server performance is uneven.
- Ip_hash each request is allocated according to the hash result of the access ip address. In this way, each visitor accesses a backend server in a fixed manner, which can solve the session problem.
- Fair (third-party) allocates requests based on the response time of the backend server. Requests with short response time are prioritized.
- Url_hash (third-party) allocates requests based on the hash result of the access url so that each url is directed to the same backend server. The backend server is effective when it is cached.
How to Implement load in Upstream Configuration:
http { upstream www.test1.com { ip_hash; server 172.16.125.76:8066 weight=10; server 172.16.125.76:8077 down; server 172.16.0.18:8066 max_fails=3 fail_timeout=30s; server 172.16.0.18:8077 backup; } upstream www.test2.com { server 172.16.0.21:8066; server 192.168.76.98:8066; } server { listen 80; server_name www.test1.com; location /{ proxy_pass http://www.test1.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name www.test2.com; location /{ proxy_pass http://www.test2.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }}
When a request arrives at www.test1.com/www.test2.com, the request is distributed to the server list set by the corresponding upstream. Each request distribution server of test2 is random, which is listed in the first case. Test1 distributes requests to the specified server based on the hashid of the ip address. That is to say, all requests from the IP address are forwarded to the specified server.
You can set different parameter controls based on the performance differences and functions of the server.
- Down indicates that the load is too heavy or not involved in the load
- If the weight of weight is too large, the load will be larger.
- The backup server is requested only when other servers are backed up or down.
- If max_fails fails more than the specified number of times, the request is paused or forwarded to another server.
- Fail_timeout
The above is a simple configuration of Nginx Server Load balancer.