0
0
NginxHow-ToBeginner · 4 min read

How to Use Round Robin Load Balancing in NGINX

To use round robin load balancing in NGINX, define an upstream block listing your backend servers. NGINX will then distribute incoming requests evenly across these servers in a rotating order by default.
📐

Syntax

The upstream block defines a group of backend servers. Each server directive inside it specifies a backend server's address. By default, NGINX uses round robin to distribute requests evenly among these servers.

The proxy_pass directive inside a location block forwards client requests to the upstream group.

nginx
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}

server {
    listen 80;

    location / {
        proxy_pass http://backend;
    }
}
💻

Example

This example shows a simple NGINX configuration that uses round robin load balancing to distribute requests evenly across three backend servers named backend1.example.com, backend2.example.com, and backend3.example.com.

nginx
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}

server {
    listen 80;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
Output
NGINX listens on port 80 and forwards incoming requests to backend1, backend2, and backend3 in a rotating order, balancing the load evenly.
⚠️

Common Pitfalls

  • Not defining an upstream block: Without an upstream block, NGINX cannot load balance.
  • Using IP addresses without ports: If backend servers listen on non-default ports, specify them explicitly.
  • Forgetting to reload NGINX: Changes to configuration require nginx -s reload to apply.
  • Not setting proxy headers: Missing headers can cause backend servers to misinterpret client info.
nginx
## Wrong (no upstream block):
server {
    listen 80;
    location / {
        proxy_pass http://backend1.example.com;
    }
}

## Right (with upstream and round robin):
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}
📊

Quick Reference

Round Robin Load Balancing Cheat Sheet:

DirectiveDescription
upstreamDefines a group of backend servers for load balancing
serverSpecifies each backend server inside the upstream block
proxy_passForwards client requests to the upstream group
proxy_set_headerSets headers to pass client info to backend
nginx -s reloadReloads NGINX to apply configuration changes

Key Takeaways

Define backend servers inside an upstream block to enable round robin load balancing.
NGINX distributes requests evenly by default without extra configuration.
Always reload NGINX after changing the configuration to apply updates.
Set proxy headers to preserve client information when forwarding requests.
Specify ports if backend servers listen on non-default ports.