English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Strategy of Load Balancing Implemented by Nginx

One: Introduction to nginx

nginx is a high-performance HTTP server and reverse proxy server. It was initially developed by Russian Igor Sysoev and still supports many large Russian websites.

Two: Three Load Balancing Strategies Supported by nginx

Round Robin: Send requests sequentially to each server.

Least Connections: Send requests to the server that holds the fewest active connections.

IP Hash: Determines which server the request is sent to by means of a hash function.

Weight: The higher the weight of the server, the greater the probability of handling requests.

Three: Round Robin Load Balancing

Add the following configuration to the nginx.conf configuration file, which provides payment services with three servers.

http {
 upstream CashServers {
  server CashServers1.com;
  server CashServers2.com;
  server CashServers3.com;
 }
 server {
  listen 80;
  location / {
   proxy_pass http://CashServers;
  }
 }
}

. Pay attention to the following points

1The default configuration is the round-robin strategy.

2.nginx load balancing supports HTTP and HTTPS protocols, you only need to modify the protocol after proxy_pass.

3.nginx supports load balancing for FastCGI, uwsgi, SCGI, and memcached, simply by changing proxy_pass to fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass.

4. This strategy is suitable for servers with similar configurations, stateless, and services that are short, flat, and fast.

4. Least Connections Load Balancing

http {
 upstream CashServers {
  least_conn;
  server CashServers1.com;
  server CashServers2.com;
  server CashServers3.com;
 }
 server {
  listen 80;
  location / {
   proxy_pass http://CashServers;
  }
 }
}

. Pay attention to the following points

1. Least connections load balancing is defined using the least_conn directive;

2. This load balancing strategy is suitable for situations where the server is overloaded due to requests with different processing times;

5. IP Hash Load Balancing

http {
 upstream CashServers {
  ip_hash;
  server CashServers1.com;
  server CashServers2.com;
  server CashServers3.com;
 }
 server {
  listen 80;
  location / {
   proxy_pass http://CashServers;
  }
 }
}

. Pay attention to the following points

1. ip hash load balancing is defined using the ip_hash directive;

2. nginx uses the IP address of the request client for hash calculation to ensure that the same server responds to the request;

3. This strategy is suitable for stateful services, such as session;

6. Weight-based Load Balancing

http {
 upstream CashServers {  
  server CashServers1.com weight=3;
  server CashServers2.com weight=2;
  server CashServers3.com weight=1;
 }
 server {
  listen 80;
  location / {
   proxy_pass http://CashServers;
  }
 }
}

. Pay attention to the following points

1. Weight-based load balancing requires the use of the weight directive to define;

2. The higher the weight, the more requests are allocated to the server that needs to be processed;

3. This strategy can be combined with the least connections load and ip hash strategies;

4. This strategy is more suitable for server hardware configurations with large differences;

7. Health Check

nginx has built-in health check mechanisms for servers. If a specific server request fails, nginx can mark it for the next time so that it will not be assigned to it again. max_fails defines the number of times to mark the server as unavailable after a specified number of failures.

That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, the website will immediately delete the content suspected of infringement.

You May Also Like