Home>Article>Operation and Maintenance> What are the nginx load balancing strategies?
In the server cluster, Nginx plays the role of a proxy server (ie, reverse proxy). In order to avoid excessive pressure on a single server, it forwards requests from users to different servers.
Nginx load balancing strategy
Load balancing is used from the list of backend servers defined from the "upstream" module Select a server to accept the user's request. A most basic upstream module is like this. The server in the module is the server list:
#动态服务器组 upstream dynamic_zuoyu { 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 configuration is completed, you want to reverse proxy the specified access to the server list:
#其他页面反向代理到tomcat容器 location ~ .*$ { index index.jsp index.html; proxy_pass http://dynamic_zuoyu; }
This is the most basic load balancing instance, but it is not enough to meet actual needs;
Currently, the upstream module of the Nginx server supports 6 ways of distribution:
Round The query method is the default load balancing strategy of the upstream module. Each request is distributed to a different backend server one by one in chronological order.
Weight method, specify the probability of polling based on the polling strategy.
ip_hash, specifies the load balancer allocation method based on the client IP. This method ensures that requests from the same client are always sent to the same server to ensure session sessions. In this way, each visitor has fixed access to a back-end server, which can solve the problem that the session cannot cross servers.
least_conn, forward the request to the backend server with fewer connections. The polling algorithm forwards requests to each backend evenly so that their loads are roughly the same; however, some requests take a long time, which will cause the backend where they are located to have a higher load. In this case, least_conn can achieve better load balancing effect.
The implementation of third-party load balancing strategies requires the installation of third-party plug-ins.
fair, allocate requests according to the response time of the server, and those with short response times will be allocated first.
url_hash, allocates requests according to the hash result of the accessed URL, so that each URL is directed to the same back-end server, and must be used in conjunction with cache hits. Multiple requests for the same resource may arrive at different servers, resulting in unnecessary multiple downloads, low cache hit rates, and a waste of resource time. Using url_hash, the same URL (that is, the same resource request) can reach the same server. Once the resource is cached, it can be read from the cache when a request is received.
For more Nginx related technical articles, please visit theNginx Usage Tutorialcolumn to learn!
The above is the detailed content of What are the nginx load balancing strategies?. For more information, please follow other related articles on the PHP Chinese website!