Nginx load balancing settings
At this stage, load balancing is a widely used technology. Nginx, as a load balancing server for http, is being used more and more widely.
There are three ways to set up Nginx load balancing:
Round-robin - This method distributes access requests to application servers in a round-robin manner.
least-connect - The next request is dispatched to the application server with the fewest active connections.
ip-hash ——Use the hash method to determine which application server to distribute the next request to.
Let’s look at these three methods separately.
Round-robin default mode
This is the simplest distribution method and the easiest to configure. Let's look at the following example.
Example 1
http {
upstream onmpw {
server 192.168.144.128;
server 192.168.144.132;
server 192.168.144.131;
}
server {
listen 80;
location / {
proxy_pass http://onmpw;
}
}
}
In Example 1, there are three application instances. Since we did not specify the load mode in the upstream, the default is round-robin mode. That is, for the first request, nginx load balancing will distribute the request to application server 128; the second request will be distributed to application server 132; the third request will be distributed to application server 131; and the fourth request will be distributed to application server 128 again. In this way, the request will be distributed to each application server in a round-robin manner.
Least connect load balancing setting method
Another load balancing method is least-connect. This method seems to be fairer to application servers. This method is advantageous when some requests take a long time to complete. In this method, the load service will try not to respond to too many requests to an overloaded application server. It will distribute new requests to application servers with less load.
The setting is as follows (just slightly change the example 1)
Example 2
upstream onmpw {
least_conn;
server 192.168.144.128;
server 192.168.144.132;
server 192.168.144.131;
}
Session persistence - load balancing
It should be noted here that whether it is round-robin or least-connect, when processing requests, both methods may distribute subsequent requests to different application servers. They cannot guarantee that the requests of the same client will always be directed to the same application server.
If we need to bind a client to a specific application server, that is, to make the client session persistent by distributing specific client requests to a specific application server, then the ip-hash load method is needed.
In this way, the client's IP address is used as the hash key to determine which application server to distribute the client's request to. This method ensures that requests from the same client will always be distributed to a specific application server, unless this specific server stops working.
To configure the ip-hash load mode, just add ip_hash to the upstream in Example 1.
Example 3
upstream onmpw {
ip_hash;
server 192.168.144.128;
server 192.168.144.132;
server 192.168.144.131;
}
Temporarily remove an application server
If an application server needs to be temporarily removed and requests are not allowed to access it, we can use down after it to mark this server, which means that nginx will not distribute requests to this application server. When the client previously responded to by this server makes a request again, nginx will automatically distribute it to other application servers.
Example 4
upstream onmpw {
server 192.168.144.128;
server 192.168.144.132;
server 192.168.144.131 down;
}
Application server weight
By default, Nginx distributes requests to the application server group in a round-robin manner. We can set the weight of each application server through the weight parameter. By default, the weight of each application is 1.
Example 5
upstream onmpw {
server 192.168.144.128 weight=5;
server 192.168.144.132;
server 192.168.144.131;
}
In Example 5, the weight of 128 is set to 5, and the weights of the other two application servers are set to 1 by default. According to this situation, for every 7 requests, 5 will be distributed to application 128, and the other two will be distributed to applications 132 and 131 respectively.
This is the purpose of setting weights for servers.
Backup application server
In actual applications, not all of our application servers need to participate. Among them, we can use one or several applications as standby servers. When other application servers have problems and cannot be accessed, Nginx will automatically start the standby application server.
Let's make some changes based on Example 5.
Example 6
upstream onmpw {
server 192.168.144.128 weight=5;
server 192.168.144.132;
server 192.168.144.131 backup;
}
The difference between this example and Example 5 is that application 131 is used as a backup server. When there are no 6 requests, 5 will be distributed to application 128 and 1 will be distributed to application 132. Only when both application servers 128 and 132 are inaccessible, Nginx will automatically distribute the request to application 131.
The above is a brief introduction to several ways to configure load balancing. I hope it will be helpful to everyone.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Nginx load balancing health_check analysis
Publish Date:2025/03/18 Views:54 Category:NETWORK
-
In Nginx load balancing, it is difficult to guarantee that every application server can run normally all the time. However, we can set Nginx to detect these application servers and detect which of them are inaccessible. There are two ways t
HTTP2 Tutorial - How to Configure HTTP2 with Nginx
Publish Date:2025/03/17 Views:195 Category:NETWORK
-
HTTP2 was officially released in 2015. If your website is still using HTTP/1.1, you may be out of date. Don't worry, here we will see how to use Nginx to upgrade your website to HTTP2. Install Nginx I feel that this column is redundant. Sin
Deep understanding of Nginx's server block selection algorithm
Publish Date:2025/03/17 Views:95 Category:NETWORK
-
Nginx is one of the most popular web servers in the world. It can successfully handle high loads with many concurrent client connections and can be used as a web server, mail server, or reverse proxy server. In this article, we will discuss
In-depth understanding of Nginx Location block matching algorithm
Publish Date:2025/03/17 Views:61 Category:NETWORK
-
Similar to the process that Nginx uses to select the Server block that will handle a request , Nginx also has an established algorithm to decide which Location block within a Server block to use to handle a request. location block syntax
Nginx - How to fix the "ssl" Directive Is Deprecated, Use "listen ... ssl"
Publish Date:2025/03/17 Views:150 Category:NETWORK
-
When updating nginx to a newer version, we may encounter deprecated configurations. Nginx uses a YAML-like definition format to create configurations. This format has evolved over time by adding, removing, or changing keywords. This article
How to fix the Unknown "connection_upgrade" Variable error in Nginx
Publish Date:2025/03/17 Views:91 Category:NETWORK
-
When using Websockets or configuring a server with nginx, we may encounter the `$connection_upgrade` variable in the nginx configuration. The $connection_upgrade variable is not available by default. However, it is recommended to define and use it in
How to fix Response Status 0 Worker Process Exited on Signal 11 in Nginx
Publish Date:2025/03/17 Views:81 Category:NETWORK
-
Actually, let's clarify first: HTTP does not have status code 0 (zero). The problem is that the nginx worker process died while handling the request, so the connection was broken, resulting in an error without any response data.
Nginx is running but not serving sites
Publish Date:2025/03/17 Views:86 Category:NETWORK
-
We recently installed nginx version 1.17 on a new machine. The configuration created in sites-available` was symlinked to `sites-enabled`, but nginx is not serving any domains.
How Nginx and uWISG servers work together
Publish Date:2025/03/17 Views:70 Category:NETWORK
-
Nginx and uWISG are two commonly used server software that can work together to provide more stable and efficient network services. This article will introduce in detail the working principle of Nginx and uWISG, and how to configure them to achieve op