Skip to content
Log In Sign Up info@muvcloud.com

A Basic Upstream Block

Define the backend servers in the http context, for example in a file under /etc/nginx/conf.d/, and point proxy_pass at the group name:

upstream app_backend {
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
    server 10.0.0.13:8080;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://app_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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Without a method directive, Nginx uses round robin: each request goes to the next server in the list. The proxy headers pass the original host, client address, and scheme to the backends. The single-backend version of this setup is in Nginx reverse proxy, and the concept is explained in what is a load balancer.

Balancing Methods

DirectiveSends each request to
(none)The next server in turn
least_connThe server with fewest active connections
ip_hashThe same server for the same client address
hash $keyA server chosen from any value

Put the directive at the top of the upstream block:

upstream app_backend {
    least_conn;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

least_conn suits requests that take very different times. hash $request_uri consistent; keeps the same URL on the same server, which helps backend caches.

Weights, Backup, and Down

upstream app_backend {
    server 10.0.0.11:8080 weight=3;
    server 10.0.0.12:8080;
    server 10.0.0.13:8080 backup;
    server 10.0.0.14:8080 down;
}
  • weight: the first server gets three requests for every one the second gets. The default weight is 1.
  • backup: receives requests only when all other servers are unavailable. It cannot be combined with ip_hash, hash, or random.
  • down: marks a server as out of service, for example during maintenance, without removing its line.

Passive Health Checks

Open-source Nginx notices a failed server only from real requests. Two parameters control it:

server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;

If three attempts fail within 30 seconds, Nginx stops sending requests to that server for 30 seconds, then tries it again. The defaults are one failure and ten seconds. The proxy_next_upstream directive decides what counts as a failure and whether the request is retried on another server. Active health checks, where Nginx polls each backend on its own schedule, are not part of the open-source build; if you need them, a dedicated balancer such as HAProxy has them.

Keepalive Connections to Backends

Before Nginx 1.29.7, which covers the packages in current distribution repositories, Nginx opens a new connection to a backend for each request unless you turn on keepalive. Reusing connections lowers the load on both sides. From 1.29.7 this is the default, and the explicit lines below do no harm:

upstream app_backend {
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
    keepalive 32;
}

location / {
    proxy_pass http://app_backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

Sessions and Sticky Clients

If the application keeps sessions in local memory or files, a user can be logged out when the next request lands on another server. ip_hash keeps most clients on one server, but many users behind one address all land on the same backend, and a client whose address changes moves. Cookie-based sticky sessions with the sticky directive became part of open-source Nginx only in version 1.29.6; older releases, including the packages in current distribution repositories, do not have it. Check yours with nginx -v. The more reliable fix is to store sessions in a shared store such as Redis or the database, so any backend can serve any request.

Test and Reload

sudo nginx -t
sudo systemctl reload nginx

Reload only after nginx -t reports that the syntax is ok. A reload keeps existing connections open. If every backend fails, clients see a 502 error: see Nginx 502 Bad Gateway, and check the error log described in Nginx logs.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations