Four modes and configurations of nginx's load balancing

1. Polling

Polling is Round Robin. According to the order in the Nginx configuration file, the client's Web requests are distributed to different backend servers in turn.
An example of the configuration is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
http{
  upstream sampleapp {
    server << dns entry or IP Address(optional with port)>>;
    server << another dns entry or IP Address(optional with port)>>;
  }
  ....
  server{
    listen 80;
    ...
    location / {
     proxy_pass http://sampleapp;
   
  }

Only 1 DNS entry above is inserted into the upstream section, namely sampleapp, which is also mentioned again in the proxy_pass section later.

2, the least connection

Web requests are forwarded to the server with the fewest connections.
An example of the configuration is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http{
   upstream sampleapp {
     least_conn;
     server << dns entry or IP Address(optional with port)>>;
     server << another dns entry or IP Address(optional with port)>>;
   }
   ....
   server{
     listen 80;
     ...
     location / {
      proxy_pass http://sampleapp;
    
   }

The above example just adds the least_conn configuration to the upstream section. Other configurations are the same as the polling configuration.

3. IP address hash

In the aforementioned two load balancing schemes, continuous web requests from the same client may be distributed to different backend servers for processing, so if a session is involved, the session will be more complicated. Common is database-based session persistence. To overcome the above difficulties, a load balancing scheme based on IP address hashing can be used. In this case, consecutive web requests from the same client will be distributed to the same server for processing.
An example of the configuration is as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
http{
   upstream sampleapp {
     ip_hash;
     server << dns entry or IP Address(optional with port)>>;
     server << another dns entry or IP Address(optional with port)>>;
   }
   ....
   server{
     listen 80;
     ...
     location / {
      proxy_pass http://sampleapp;
    
   }

The above example just adds the ip_hash configuration to the upstream section. Other configurations are the same as the polling configuration.

4. Weight-based load balancing

基于权重的负载均衡即Weighted Load Balancing,这种方式下,我们可以配置Nginx把请求更多地分发到高配置的后端服务器上,把相对较少的请求分发到低配服务器。
配置的例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
http{
   upstream sampleapp {
     server << dns entry or IP Address(optional with port)>> weight=2;
     server << another dns entry or IP Address(optional with port)>>;
   }
   ....
   server{
     listen 80;
     ...
     location / {
      proxy_pass http://sampleapp;
     }
  }

上面的例子在服务器地址和端口后weight=2的配置,这意味着,每接收到3个请求,前2个请求会被分发到第一个服务器,第3个请求会分发到第二个服务器,其它的配置同轮询配置。

还要说明一点,基于权重的负载均衡和基于IP地址哈希的负载均衡可以组合在一起使用。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325373488&siteId=291194637