Skip to main content
In this tutorial, you’ll learn how to configure Nginx as a load balancer using three common algorithms:
  1. Round Robin: Distributes requests evenly across all backend servers.
  2. Weighted Round Robin: Assigns traffic proportionally based on server weights.
  3. IP Hash: Routes each client IP to the same backend to maintain session persistence.
The image illustrates a Round Robin load balancing algorithm using NGINX, distributing requests to two Apache web servers.
The image illustrates a weighted round robin load balancing algorithm using NGINX, distributing traffic to two Apache web servers with different weights.
The image illustrates an IP Hash algorithm used in load balancing, showing a load balancer directing traffic to multiple web servers based on IP addresses.

Load Balancing Algorithm Comparison

Environment Setup

We have three Ubuntu nodes in our test lab:
  • nginx: Nginx load balancer (192.230.202.10)
  • node01 & node02: Apache backend servers
On each backend, Apache is running and UFW is active but only port 22 is open by default:
For security, restrict HTTP access so only the load balancer (192.230.202.10) can reach port 80 on your backends.

Configuring the Load Balancer

On the nginx node, create or edit your site configuration:
Start with a basic server block:

1. Round Robin

Add an upstream block for your backend pool:
Enable the site and reload Nginx:
Test with curl to see alternating responses:

2. Weighted Round Robin

Adjust the upstream block to assign server weights:
Reload Nginx to apply:
Now about 10× more requests go to the higher-weighted server.

3. IP Hash

Enable ip_hash for session stickiness by client IP:
Reload Nginx:
With ip_hash, each client IP always hits the same backend server. This is ideal for stateful applications.

You can extend this setup with health checks, SSL termination, or alternative Nginx modules.

Watch Video

Practice Lab