Nginx 4 kinds of schemes for load balancing _nginx

Source: Internet
Author: User
Tags sessions dns entry

1. Polling

Polling is round Robin, which distributes the client's Web requests to different back-end servers sequentially, according to the order in the Nginx configuration file.
Examples of configurations are 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; 
   ... 
   Location/{ 
    proxy_pass http://sampleapp; 
   }  
 } 

Only 1 DNS portals were inserted into the upstream section, the SampleApp, which was also recalled in the Proxy_pass section later.

2, the least connection

Web requests are forwarded to the server with the fewest number of connections.
Examples of configurations are 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; 
    ... 
    Location/{ 
     proxy_pass http://sampleapp; 
    }  
  } 

The above example simply adds a least_conn configuration to the upstream section. The other configuration is the same polling configuration.

3, IP address hash

In the two previous load balancing scenarios, successive Web requests of the same client may be distributed to different back-end servers for processing, so sessions are more complex if session sessions are involved. It is common for database-based session persistence. To overcome the above challenges, you can use a load balancing scheme based on IP address hashing. In this way, successive Web requests of the same client are distributed to the same server for processing.
Examples of configurations are 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; 
    ... 
    Location/{ 
     proxy_pass http://sampleapp; 
    }  
  } 

The above example simply adds a ip_hash configuration to the upstream section. The other configuration is the same polling configuration.

4. Load balancing based on weight

Load balancing based on weight is weighted load balancing, in which case we can configure Nginx to distribute requests more to highly configured back-end servers and distribute relatively small requests to lower-distribution servers.
Examples of configurations are 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; 
    ... 
    Location/{ 
     proxy_pass http://sampleapp; 
    } 
 } 

The above example weight=2 configuration after the server address and port, which means that the top 2 requests are distributed to the first server for each 3 requests, and the 3rd request is distributed to the second server, and the other configuration is the same polling configuration.

It is also important to note that weight-weighted load balancing and load balancing based on IP address hashes can be combined together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.