Load balancing is an important technique for scaling web applications and improving their availability and performance. It distributes incoming traffic across multiple servers, allowing them to handle more requests and avoid overload.

Nginx, the popular open-source web server software, has powerful load balancing capabilities, making it a favourite among system administrators and web developers. In this blog post, we'll explore Nginx's load balancing features, including the supported load balancing methods, and provide a technical demo to help you set up each method.

To get started, you'll need a Linux System. For the demo, I'll be using Ubuntu 20.04. You are free to choose any OS of your liking.

Installing Nginx

The packages provided by the official Ubuntu repository may be outdated, so we will use the packages provided by Nginx themselves. For that, we will need to configure APT Package Manager to go out and fetch packages from the Nginx repository for our OS.  

Run the commands below to set up Nginx for Ubuntu 20.04.

echo "deb https://nginx.org/packages/ubuntu/ focal nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
echo "deb-src https://nginx.org/packages/ubuntu/ focal nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
wget http://nginx.org/keys/nginx_signing.key && sudo apt-key add nginx_signing.key
sudo apt update && sudo apt install nginx

While we're at it, let's also create a directory to store the Nginx configuration files.

mkdir /etc/nginx/{sites_available,sites_enabled}

Now we'll create four files with the names lb-demo.local , least-conn.lb-demo.local, ip-hash.lb-demo.local and wrb.lb-demo.local in sites_available. We'll then symlink it to sites_enabled.

touch /etc/nginx/sites_available/{lb-demo.local,least-conn.lb-demo.local,ip-hash.lb-demo.local,wrb.lb-demo.local}
ln -s /etc/nginx/sites_available/* /etc/nginx/sites_enabled/

Finally, add the following directive into the http block inside the nginx.conf file.

include /etc/nginx/sites_enabled/*;

After adding the directive, the file should look something like this:

http {
  ...
  ...
  ...
  
  include /etc/nginx/sites_enabled/*;

}
Example: nginx.conf file
✍️
Note that the http block is present by default in an Nginx installation. However, you need to add the include directive at the bottom of the http block.
ℹ️
Do not forget to add an entry to the host's file pointing to the domain lb-demo.local towards 127.0.0.1 if you're following along in your local system.
For example:
127.0.0.1 localhost
::1 localhost
127.0.0.1 lb-demo.local

Supported Load Balancing Methods

Nginx supports several load balancing methods, each with its own strengths and weaknesses. The most commonly used methods are listed below.

✍️
You could run a few docker containers of httpd, caddy , and openlitespeed as backends for this demo.

Round-robin

The round-robin method is the simplest and most widely used load balancing algorithm. It distributes incoming requests evenly across a set of servers in a cyclic order. Each server is given an equal number of requests, so the load is balanced evenly. This method is best suited for applications with similar server capabilities and workloads.

Here's an example of Nginx configuration to use the round-robin method.

http {
  upstream backend {
    server localhost:82;
    server localhost:83;
    server localhost:84;
  }

  server {
    listen 80;
    server_name lb-demo.local;

    location / {
      proxy_pass http://backend;
    }
  }
}
Round-robin LB Configuration
ℹ️
Add the weight=n parameter beside the backend URL, where n is a natural number starting from 1. This will let us perform weight-based round robin load balancing.

For example:
server localhost:83 weight=2;

Now, Nginx will distribute incoming requests to the backend servers based on their weights. Servers with higher weights will receive more requests than servers with lower weights.

In this configuration, the upstream directive defines a group of backend servers. The proxy_pass directive forwards requests to the specified backend servers in a round-robin fashion.

Copy and paste the above config into the lb-demo.local file and run the nginx -s reload command.

Now every time we open the domain http://lb-demo.local, a different page will be served to us due to the round robin load balancing method.

Least connections

The least connections method distributes incoming requests to the server with the fewest active connections. It ensures that the load is evenly distributed among the available servers, even if their capacities differ. This method is ideal for applications with varying server capacities or workloads.

Here's an example of Nginx configuration to use the least connections method.

http {
  upstream backend {
    least_conn;
    server localhost:82;
    server localhost:83;
    server localhost:84;
  }

  server {
    listen 80;
    server_name lb-demo.local;

    location / {
      proxy_pass http://backend;
    }
  }
}
Least Connection LB Configuration

In this configuration, the least_conn directive is used to instruct Nginx to use the least connections method. The rest of the configuration is similar to the round-robin example.

Copy and paste the above configuration into the least-conn.lb-demo.local file and run the nginx -s reload command.

Now every time we open the domain http://lb-demo.local, the backend with the least number of live connections will be served to us.

IP hash

The IP hash method distributes incoming requests based on the client's IP address. In this method, the requests from the same client (i.e. same IP Address as the last request) are always sent to the same backend server. This is useful for applications that require session persistence, such as e-commerce sites or online banking applications.

Here's an example of Nginx configuration to use the IP hash method.

http {
  upstream backend {
    ip_hash;
    server localhost:82;
    server localhost:83;
    server localhost:84;
  }

  server {
    listen 80;
    server_name lb-demo.local;

    location / {
      proxy_pass http://backend;
    }
  }
}
IP Hash LB Configuration

In this configuration, the ip_hash directive is used to instruct Nginx to use the IP hash method. The rest of the configuration is similar to the round-robin example.

Copy and paste the above configuration into the lb-demo.local file and run the nginx -s reload command.

Access the domain http://lb-demo.local once and refresh the page again. The backend previously served to us will be served again by Nginx.

Lastly,

you now clearly understand the four load balancing methods: round-robin, weighted round-robin, least connections, and IP hash. You can also set up the different load balancing methods with Nginx with the help of the configuration files we wrote in this article.

Please comment below if you have any queries, I try to regularly update my articles to ensure legibility!