---
title: How to Configure a Load Balancer on Nginx
description: "Here’s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers. This setup is commonly used to improve application performance, ensure availability, and support redundancy. I will use an Ubuntu server…"
url: "https://truehost.com/support/knowledge-base/how-to-configure-a-load-balancer-on-nginx/"
updated: "2024-11-02"
category: Cloud servers in Kenya
---

# How to Configure a Load Balancer on Nginx

Here’s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers.

This setup is commonly used to improve application performance, ensure availability, and support redundancy.

I will use an Ubuntu server for this article.

### Step 1: Install Nginx

If you haven’t installed Nginx, start by installing it on the server you’ll use as your load balancer.

#### Update the System Packages

```
sudo apt update
```

#### Install Nginx

On Debian-based systems like Ubuntu, you can install Nginx with:

```
sudo apt install -y nginx
```

On RHEL-based systems, like Almalinux, CentOs use:

```
sudo dnf install -y nginx
```

After installation, start and enable Nginx:

```
sudo systemctl start nginx
sudo systemctl enable nginx
```

### Step 2: Configure the Load Balancer

#### Open the Nginx Configuration File

Edit the Nginx configuration file to define the load balancer settings.

Use either the default configuration file at `/etc/nginx/nginx.conf` or create a new one in the `/etc/nginx/conf.d/` directory.

```
sudo nano /etc/nginx/conf.d/load_balancer.conf
```

#### Define the Upstream Servers

In the configuration file, define the backend servers (also called “upstream servers”) that will handle the load. Add the following section, replacing the IP addresses with those of your backend servers:

```
upstream backend_servers {server 192.168.1.10; # Server 1server 192.168.1.11; # Server 2server 192.168.1.12; # Server 3}
```

#### Set Up the Load Balancing Configuration

Now configure a server block to route incoming requests to the defined `upstream` servers:

```
server {
    listen 80;

    location / {
        proxy_pass http://backend_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

This configuration instructs Nginx to listen on port 80 and forward all traffic to the servers defined in the `upstream backend_servers` block.

### Step 3: Load Balancing Algorithms

Nginx offers several load balancing algorithms. The default algorithm is **Round Robin**, but you can also configure it for **Least Connections** or **IP Hash**.

1. **Round Robin (default)** – Distributes traffic equally among all servers:

```
upstream backend_servers {server 192.168.1.10;server 192.168.1.11;}
```

2. **Least Connections** – Directs traffic to the server with the fewest active connections, which helps balance load dynamically:

```
upstream backend_servers {least_conn;server 192.168.1.10;server 192.168.1.11;}
```

3. **IP Hash** – Sends requests from the same client IP to the same backend server, which can be useful for session persistence:

```
upstream backend_servers {ip_hash;server 192.168.1.10;server 192.168.1.11;}
```

Choose the algorithm that best suits your needs and adjust the `upstream` block accordingly.

### Step 4: Enable Health Checks (Optional)

Nginx doesn’t natively support active health checks in the free version, but you can set a passive health check by configuring `proxy_next_upstream` directives to route requests away from failed servers.

```
upstream backend_servers {
    server 192.168.1.10;
    server 192.168.1.11;
    server 192.168.1.12;
}

server {
    listen 80;

    location / {
        proxy_pass http://backend_servers;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

In this configuration, Nginx will skip to the next server if it encounters errors like a timeout or a 500-series HTTP error.

### Step 5: Apply and Test the Configuration

#### Check Nginx Syntax

Run the following command to check the syntax of your Nginx configuration:

```
sudo nginx -t
```

If the syntax is correct, restart Nginx to apply the changes:

```
sudo systemctl restart nginx
```

#### Verify the Load Balancer

Access the IP address or domain of your Nginx server (the load balancer) from a web browser to confirm that traffic is correctly routed to the backend servers. You can also use tools like `curl` or `ab` (Apache Bench) to send multiple requests and confirm that they are distributed across servers.

```
curl http://your_load_balancer_ip
```

### Step 6: Additional Configuration (Optional)

You can further customize the load balancer by:

- **Configuring SSL** for secure connections
- **Setting a custom error page** for when all backend servers are down
- **Adding rate limiting** to protect your backend servers

## Author's Final Word

This configuration gives you a robust Nginx load balancer setup to handle incoming requests and distribute them across multiple backend servers, improving your application’s scalability and reliability
