Log inSign up

Balance traffic across your servers with Nginx upstreams

Run Nginx on one VPS, list your app servers in an upstream block by their public IPs, and point proxy_pass at that group. Choose round robin, least_conn or ip_hash, set max_fails so dead nodes are skipped, and allow the app port on each backend only from the balancer address. Encrypt the hop with HTTPS or WireGuard, since it crosses the public internet.

Topic: Web servers and proxies · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does the layout look like without a private network?

One server is the entry point and the others do the work. BitcoinVPS gives each server a dedicated public IPv4 and IPv6 address but no private network between them, so the balancer reaches its backends over those public addresses. In this guide:

  • 198.51.100.24: the balancer running Nginx, where example.net points.
  • 203.0.113.10, 203.0.113.11, 203.0.113.12: three app servers listening on port 8080.

Deploy the backends in the same city as the balancer to keep round trips short; the cities page lists the locations. Each backend needs the same app version and config, and any state such as sessions or uploads must live somewhere all of them can reach, like a shared database or Redis server. Otherwise a visitor who lands on another node loses their login.

How do you install Nginx on the balancer?

The balancer only needs Nginx from the distro packages. The Nginx install guide walks through every step; the short form is:

# Ubuntu and Debian
sudo apt update && sudo apt install -y nginx
sudo ufw allow 'Nginx Full'

# Rocky and Alma
sudo dnf install -y nginx
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo setsebool -P httpd_can_network_connect 1

sudo systemctl enable --now nginx

The SELinux boolean on Rocky and Alma lets Nginx open outbound connections to the backends. Without it every proxied request ends in a 502 and an AVC denial in the audit log.

How do you define the upstream group?

Declare the backends once inside the http context, then reference the group name from a server block. Create /etc/nginx/conf.d/app-upstream.conf:

upstream app_backends {
    least_conn;
    server 203.0.113.10:8080 max_fails=3 fail_timeout=20s;
    server 203.0.113.11:8080 max_fails=3 fail_timeout=20s;
    server 203.0.113.12:8080 max_fails=3 fail_timeout=20s backup;
    keepalive 32;
}

The site file then proxies to the group. Note the two lines that make upstream keep-alive work:

server {
    listen 80;
    listen [::]:80;
    server_name example.net;

    location / {
        proxy_pass http://app_backends;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        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;
        proxy_next_upstream error timeout http_502 http_503;
        proxy_next_upstream_tries 2;
    }
}

Test and reload with sudo nginx -t && sudo systemctl reload nginx, then add HTTPS on the balancer with sudo certbot --nginx -d example.net as in the Certbot guide.

Which balancing method should you pick?

Pick the method by how requests behave, not by habit. Nginx supports these in the open source build:

  • Round robin (no directive): requests rotate in order. Good when every request costs about the same.
  • least_conn: the next request goes to the server with the fewest active connections. Better when some requests are slow, such as reports or uploads.
  • ip_hash: a client address always maps to the same server. A simple form of sticky sessions, though many visitors behind one carrier NAT will land together.
  • hash $request_uri consistent: the same URL goes to the same node, which helps per-node caches.
  • random two least_conn: picks two servers at random and uses the less busy one.

Add weight=2 to a server line to send it twice the share, useful when one backend is a larger plan than the others.

How does failover work?

Open source Nginx uses passive health checks: it notices failures in real traffic. When a server fails max_fails times within fail_timeout, Nginx stops sending it requests for the length of fail_timeout, then tries again. proxy_next_upstream lets a failed request retry on another node, and proxy_next_upstream_tries caps the retries so one bad request cannot bounce across the whole pool.

A server marked backup only receives traffic when all primary servers are unavailable. Mark a server down to drain it during maintenance: edit the line, reload, wait for open requests to finish, then update the node. Retries are safe for reads but can repeat a non-idempotent POST; Nginx by default does not retry POST requests once they were sent to a backend, which is the behavior you want for payments and orders.

How do you protect the backends?

The backends must only answer the balancer. Otherwise anyone can skip it and hit the app port directly. On each backend, allow port 8080 from 198.51.100.24 only:

# Ubuntu and Debian backends
sudo ufw allow OpenSSH
sudo ufw allow from 198.51.100.24 to any port 8080 proto tcp
sudo ufw enable

# Rocky and Alma backends
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.24" port port="8080" protocol="tcp" accept'
sudo firewall-cmd --reload

Next, encrypt the hop. Two options work well. You can run TLS on the backends and proxy with proxy_pass https://app_backends; plus proxy_ssl_verify on;, proxy_ssl_server_name on; and proxy_ssl_trusted_certificate pointing at the system CA bundle (/etc/ssl/certs/ca-certificates.crt on Debian family, /etc/pki/tls/certs/ca-bundle.crt on Rocky and Alma). Or you can build a WireGuard tunnel from the balancer to each node, as in the WireGuard guide, and list the tunnel addresses in the upstream instead.

How do you verify that traffic is spread?

Log which backend served each request, then send a burst of requests. Add a log format to the http context on the balancer:

log_format upstream '$remote_addr "$request" $status '
                    'up=$upstream_addr rt=$upstream_response_time';
access_log /var/log/nginx/lb.log upstream;

Reload and test from your computer:

for i in $(seq 1 12); do curl -s -o /dev/null -w "%{http_code}\n" https://example.net/; done
sudo tail -n 12 /var/log/nginx/lb.log

The up= column should show the primary servers taking turns. Stop the app on one node and repeat: requests move to the others after the failure limit, and the stopped node returns on its own once the app is back.

What should you check when things go wrong?

Most problems show up in the balancer error log, /var/log/nginx/error.log.

  • no live upstreams: every backend is marked failed. Test one directly from the balancer with curl -I http://203.0.113.10:8080/.
  • connect() failed (13: Permission denied) on Rocky or Alma: the SELinux boolean is off.
  • Users get logged out: sessions live in local memory on each node. Move them to a shared store or use ip_hash.
  • Apps build wrong links: the app ignores X-Forwarded-Proto. Turn on its trusted proxy option.

Keep an eye on transfer too: every response crosses the balancer, so its plan should have enough monthly transfer for the whole site. Monitoring usage shows how to track it.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2a balancer for a few app nodes1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4app backend nodes2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8busier backends or a balancer with TLS on many hosts4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about Load balancing with Nginx upstreams

Is there a built-in load balancer I can rent instead?

No. BitcoinVPS does not sell a load balancer product, so you run one yourself on a VPS with Nginx or HAProxy.

Can the backends talk over a private network?

There is no private network between servers. They connect over their public IPs, which is why this guide restricts ports by source address and encrypts the traffic.

Does open source Nginx do active health checks?

No, it relies on passive checks from real requests. If you need active probes, HAProxy includes them; see the HAProxy guide.

Does the balancer become a single point of failure?

Yes. Keep its config in version control and backups so you can rebuild it quickly, and keep it small and dedicated to balancing.

Should TLS end on the balancer or on the backends?

End public TLS on the balancer, where Certbot runs. Use a second TLS layer or a tunnel for the hop to the backends because it crosses the internet.