Nginx Load Balancing
Luke Hebert
- 3 minutes read - 630 wordsLoad balancing spreads incoming traffic across multiple servers so no single machine becomes a bottleneck. This guide walks through configuring Nginx as a round-robin load balancer, then covers weighted distribution, IP hashing, and automatic failover so you can match traffic routing to your actual infrastructure needs.
About Load Balancing
Load balancing is a useful mechanism to distribute incoming traffic across several capable virtual private servers. By apportioning the processing workload to multiple machines, you gain redundancy, fault tolerance, and improved stability. The Round Robin algorithm sends visitors to one of a set of IPs in sequence. At its most basic level, Round Robin is easy to implement and distributes server load without factoring in more nuanced variables like server response time or the visitor’s geographic region.
Setup
The steps in this tutorial require root privileges on your VPS. You can see how to set that up in the Users Tutorial.
Prior to setting up Nginx load balancing, you should have Nginx installed on your VPS. You can install it quickly with apt-get:
sudo apt-get install nginxUpstream Module
To set up a round-robin load balancer, you will use the Nginx upstream module. The configuration is incorporated directly into the Nginx settings.
Open your website’s configuration (in the examples below, I work off the generic default virtual host):
sudo nano /etc/nginx/sites-available/defaultAdd the load balancing configuration to the file. First, define the upstream block:
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}Then reference the upstream in your server block:
server {
location / {
proxy_pass http://backend;
}
}Restart Nginx:
sudo service nginx restartAs long as all of the virtual private servers are in place, the load balancer will begin distributing visitors to the linked servers equally.
Directives
The previous section covered how to distribute load equally across several virtual servers. However, there are many reasons why equal distribution may not be the most efficient approach. Nginx provides several directives to direct site visitors more effectively.
Weight
One way to allocate users to servers with more precision is to assign a specific weight to certain machines. Nginx lets you specify the proportion of traffic that should be directed to each server.
A load-balanced setup that includes server weight could look like this:
upstream backend {
server backend1.example.com weight=1;
server backend2.example.com weight=2;
server backend3.example.com weight=4;
}The default weight is 1. With a weight of 2, backend2 receives twice as much traffic as backend1, and backend3, with a weight of 4, handles twice as much traffic as backend2 and four times as much as backend1.
Hash
IP hash allows servers to respond to clients based on their IP address, sending visitors back to the same VPS each time they visit (unless that server is down). If a server is known to be inactive, mark it as down. All IPs that were supposed to be routed to the down server are then directed to an alternate one.
The configuration below provides an example:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}Max Fails
Under the default round-robin settings, Nginx will continue sending data to virtual private servers even if they are not responding. The max_fails directive can automatically prevent this by rendering unresponsive servers inoperative for a set amount of time.
Two parameters control this behavior:
- max_fails - the maximum number of failed connection attempts before a server is considered inactive.
- fail_timeout - the duration the server is considered inoperative. Once the timeout expires, new attempts to reach the server resume. The default timeout value is 10 seconds.
A sample configuration might look like this:
upstream backend {
server backend1.example.com max_fails=3 fail_timeout=15s;
server backend2.example.com weight=2;
server backend3.example.com weight=4;
}