Home > Article > Operation and Maintenance > What are the modes for nginx to implement load balancing?
This article comes from the nginx tutorial. It introduces several modes of nginx to achieve load balancing. It has certain reference value and I hope it can help everyone. nginx implements load balancing mode: 1. Polling; 2. ip_hash; 3. url_hash; 4. fair.
(Recommended tutorial: nginx tutorial)
nginx has several modes to implement load balancing:
1. Polling
Each request is assigned to different back-end servers one by one in chronological order, which is also the default mode of nginx. The configuration of polling mode is very simple, just add the server list to the upstream module.
The following configuration means: there are three servers in the load. When a request arrives, nginx allocates the request to the three servers in chronological order.
upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; }
2. ip_hash
Each request is assigned according to the hash result of the accessed IP. The same IP client always accesses a back-end server. It can ensure that requests from the same IP are sent to a fixed machine, which can solve the session problem.
The following configuration refers to: There are three servers in the load. When a request arrives, nginx gives priority to allocation according to the result of ip_hash, that is, the request for the same IP is fixed on a certain server, and the others are allocated according to the result of ip_hash. Time sequence distributes requests to three servers for processing.
upstream serverList { ip_hash server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; }
3. url_hash
Requests are allocated according to the hash result of the accessed URL. The same URL is always forwarded to the same back-end server for processing.
upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; hash $request_uri; hash_method crc32; }
4, fair
Requests are allocated according to the response time of the back-end server, and those with short response times are allocated first.
upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; fair; }
The above is the detailed content of What are the modes for nginx to implement load balancing?. For more information, please follow other related articles on the PHP Chinese website!