Summary of load balancing strategy of Nginx server
1, On load balancing of Nginx
In the server cluster, Nginx acts as a proxy server (i.e. reverse proxy). In order to avoid excessive pressure on a single server, it forwards requests from users to different servers.
2, Nginx load balancing strategy
Load balancing is used to select a server from the list of back-end servers defined in the "upstream" module to accept user requests. The most basic upstream module is as follows. The server in the module is the server list:
#Dynamic server group upstream dynamic_router { server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 }
After the upstream module is configured, the specified access reverse proxy should be sent to the server list:
#Reverse proxy other pages to tomcat container location ~ .*$ { index index.jsp index.html; proxy_pass http://dynamic_router ; }
This is the most basic example of load balancing, but it is not enough to meet the actual needs; At present, the upstream module of the Nginx server supports six modes of allocation:
polling | Default mode |
weight | Weight method |
ip_hash | According to ip distribution mode |
least_conn | Minimum connection mode |
fair (third party) | Response time mode |
url_hash (third party) | Assign by URL |
Here, only the load balancing strategy provided by Nginx will be described in detail, which will not be described by the third party.
1. Polling
The most basic configuration method is polling, which is the default load balancing policy of the upstream module. Each request is assigned to a different back-end server in chronological order.
There are the following parameters:
fail_timeout | And Max_ Failure. |
max_fails | In fail settings_ The maximum number of failures within the time set by the timeout parameter. If all requests to the server fail within this time, the server is considered to be down, |
fail_time | The length of time that the server will be considered to be down. The default is 10s. |
backup | Mark the server as an alternate server. When the primary server stops, the request is sent to it. |
down | Mark that the server is permanently down. |
Note:
- During polling, if the server goes down, the server will be automatically eliminated.
- The default configuration is the polling policy.
- This strategy is suitable for the use of server configuration, stateless and short and fast services.
2,weight
The weight method specifies the probability of polling based on the polling strategy. Examples are as follows:
#Dynamic server group upstream dynamic_router { server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082 backup; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
In this example, the weight parameter is used to specify the polling probability. The default value of weight is 1,; The value of weight is directly proportional to the access ratio. For example, Tomcat 7.0 is twice as likely to be accessed as other servers.
Note:
- The higher the weight, the more requests are allocated to be processed.
- This policy can be used with least_conn and IP_ Use with hash.
- This strategy is more suitable for the situation where the hardware configuration of the server is quite different.
3,ip_hash
Specifies that the load balancer is distributed based on the client IP. This method ensures that the requests of the same client are sent to the same server to ensure the session. In this way, each visitor has a fixed access to a back-end server, which can solve the problem that sessions cannot cross servers.
#Dynamic server group upstream dynamic_router { ip_hash; #Ensure that each visitor has fixed access to a back-end server server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
Note:
- Before nginx version 1.3.1, you cannot use IP_ weight is used in hash.
- ip_hash cannot be used with backup.
- This strategy is suitable for stateful services, such as session.
- When a server needs to be removed, it must be manually down.
4,least_conn
Forward the request to the back-end server with fewer connections. Polling algorithm is to forward the request to each back-end evenly, so that their load is roughly the same; However, some requests take a long time, which will lead to a high load on the back end. In this case, least_conn this way can achieve better load balancing effect.
#Dynamic server group upstream dynamic_router { least_conn; #Forward the request to the back-end server with fewer connections server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082 backup; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
Note:
- This load balancing strategy is suitable for server overload caused by different request processing times.
5. Third party strategy
The implementation of the third-party load balancing strategy requires the installation of third-party plug-ins.
①fair
Requests are allocated according to the response time of the server, and those with short response time are allocated first.
#Dynamic server group upstream dynamic_router { server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 fair; #Achieve priority allocation with short response time }
②url_hash
Allocate the request according to the hash result of the access url, so that each url is directed to the same back-end server, which should be used in conjunction with the cache hit. Multiple requests for the same resource may reach different servers, resulting in unnecessary multiple downloads, low cache hit rate and waste of some resources and time. Instead, use the url_hash can make the same url (i.e. the same resource request) reach the same server. Once the resource is cached and the request is received, it can be read from the cache.
#Dynamic server group upstream dynamic_router { hash $request_uri; #Each url is directed to the same back-end server server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 }
3, Summary
The above are the implementation methods of six load balancing strategies. In addition to polling and polling weight, they are implemented by Nginx according to different algorithms. In practical application, it needs to be used selectively according to different scenarios, mostly a combination of multiple strategies to meet the actual needs.