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
How to Install Nginx on Ubuntu 20.04?
Publish Date:2025/04/07 Views:157 Category:OPERATING SYSTEM
-
Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.
How to use Let's Encrypt with Nginx to configure https in Ubuntu 20.04
Publish Date:2025/04/07 Views:123 Category:OPERATING SYSTEM
-
Let's Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on your web server. It simplifies the process by providing a software client, Certbot, which a
Install WordPress with Nginx on Ubuntu 18.04
Publish Date:2025/04/07 Views:86 Category:OPERATING SYSTEM
-
WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
Nginx load balancing health_check analysis
Publish Date:2025/03/18 Views:56 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:98 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:65 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