Front-end technology sharing: Nginx load balancing video, basic practical applications

Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, and is issued under a BSD-like protocol. Its characteristics are that it occupies less memory, has strong concurrent processing capabilities, and is known for its high performance and low resource consumption. A large number of domestic websites use Nginx, such as Baidu, Jingdong, Sina, Netease, Tencent, Taobao, etc. Since we inevitably have to contact and use such a high-performance web server in our daily work, it is very necessary for us to learn and master Nginx.

4 configuration examples of Nginx load balancing schemes. This article explains polling, minimum connections, IP address hashing, weight-based load balancing, etc. Friends who need it can refer to the following:

1. Polling

Round Robin is Round Robin, which distributes the client's Web requests to different back-end servers in turn according to the order in the Nginx configuration file.

The configuration example is as follows:

2.png

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

2. The least connection

Web requests will be forwarded to the server with the least number of connections.

The configuration example is as follows:

3.png

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

3. IP address hash

In the aforementioned two load balancing schemes, consecutive web requests from the same client may be distributed to different back-end servers for processing. Therefore, if a session is involved, the session will be more complicated. Common is the persistence of database-based sessions. To overcome the above problems, you can use a load balancing scheme based on IP address hashing. In this case, consecutive web requests from the same client will be distributed to the same server for processing.

The configuration example is as follows:

4.png

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

4. Load balancing based on weight

Weight-based load balancing is Weighted Load Balancing. In this way, we can configure Nginx to distribute more requests to high-configuration back-end servers, and relatively few requests to low-configuration servers.

The configuration example is as follows:

5.png

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. , Other configurations are the same as polling configuration.

It should also be noted that weight-based load balancing and IP address hash-based load balancing can be used in combination.

This article is from Qianfeng Education: http://wh.mobiletrain.org/ , please indicate the source for reprinting.

Guess you like

Origin blog.51cto.com/15128702/2665350