4 methods of Nginx 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:

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:

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:

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

Weight-based load balancing is Weighted Load Balancing. In this way, we can configure Nginx to distribute more requests to high-configured backend servers, and distribute relatively fewer requests to low-profile servers.
An example of the configuration is as follows:

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;
    }
 }

The above example is configured with weight=2 after the server address and port, which means that for every 3 requests received, the first 2 requests will be distributed to the first server, and the third request will be distributed to the second server. , and other configurations are the same as the polling configuration.

Also note that weight-based load balancing and IP address hash-based load balancing can be used in combination.

Guess you like

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