NEWUse coupon code SMX20 to get 20% OFF your first billing cycle on any game server or VPS!Explore Store

VPS HOSTING

How to Set Up a Load Balancer Across Multiple VPS Servers with Nginx

Spread traffic over several application VPS with an Nginx load balancer: upstream methods, passive health checks, sessions, HTTPS termination, private networking, shared state and when to scale up instead.

When one application server is no longer enough, or you want the site to survive a server failure, the next step is several application servers behind a load balancer. Nginx can do this with a few lines of configuration. The configuration is the easy part, though: the real work is making the application stateless so any server can handle any request.

First: Do You Need to Scale Out?

Adding servers adds complexity: more machines to patch, deploys that must reach all of them, and a load balancer that is itself a single point of failure unless you double it. Moving to a larger VPS (vertical scaling) is often simpler and is enough for a surprising amount of traffic. Scale out when:

  • one server is near its limit even on a large plan, or the workload is CPU-bound and parallelises well;
  • you need the site to stay up while one server is being patched or fails;
  • you want to deploy without downtime by draining one server at a time.

Often the database is the real bottleneck. Measure first (see our monitoring guide); more app servers do not help if every request waits on the same database.

Architecture

                    ┌─▶ app1 (10.0.0.11:8000)
Internet ─HTTPS─▶ LB ┼─▶ app2 (10.0.0.12:8000) ──▶ database (10.0.0.20)
   (Nginx :443)      └─▶ app3 (10.0.0.13:8000) ──▶ Redis (sessions, cache)

Use private networking between the servers if your provider offers it; otherwise restrict each app server's port to the load balancer's IP with a firewall rule and encrypt the traffic (TLS to the backends, or a WireGuard tunnel) because it crosses the public network.

Step 1: The Upstream Block

# /etc/nginx/conf.d/app-upstream.conf on the load balancer
upstream app_backend {
    least_conn;
    server 10.0.0.11:8000 max_fails=3 fail_timeout=15s;
    server 10.0.0.12:8000 max_fails=3 fail_timeout=15s;
    server 10.0.0.13:8000 max_fails=3 fail_timeout=15s;
    keepalive 32;
}
MethodHow it choosesGood for
(default) round robinEach server in turn, optionally weightedSimilar servers, short similar requests
least_connServer with the fewest active connectionsRequests of varying length (the usual best default)
ip_hashSame client IP to the same serverLegacy apps that keep sessions in local memory
hash $key consistentSame key to the same serverCache locality, for example by URL

Add weight=2 to give a larger server more traffic, backup for a server used only when others fail, and down to take one out of rotation for maintenance.

Step 2: The Load Balancer Server Block

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://app_backend;
        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;
        proxy_connect_timeout 3s;
    }
}

Then add HTTPS on the load balancer only (sudo certbot --nginx -d example.com). The app servers receive plain HTTP from the load balancer and must trust its X-Forwarded-* headers, but only from the load balancer's IP.

Step 3: Health Checks

Open-source Nginx uses passive health checks: if a server fails max_fails times within fail_timeout, Nginx stops sending it traffic for fail_timeout, then tries again. proxy_next_upstream retries a failed request on another server.

Two cautions:

  • Only retry requests that are safe to repeat. By default Nginx does not retry non-idempotent requests (such as POST) after they have been sent to an upstream, which protects you from duplicate orders; keep it that way.
  • Active health checks (probing /health on a schedule) are a feature of NGINX Plus. With open-source Nginx, combine passive checks with your monitoring system, or use HAProxy, which has active checks built in.

Step 4: Make the Application Stateless

This is what actually makes load balancing work:

StateProblem with several serversFix
Sessions in local files or memoryUsers are logged out when they hit another serverStore sessions in Redis or the database (or use ip_hash as a stopgap)
Uploaded files on local diskFiles exist on only one serverObject storage (S3-compatible), or a shared file server
Local cachesEach server has different cached dataShared Redis cache (see our Redis guide)
Cron jobs on every serverJobs run three timesRun scheduled jobs on one designated server, or use a lock
Different code versionsInconsistent behaviour mid-deployDeploy to all servers with one script; drain servers one at a time

Step 5: Rolling Deploys

A load balancer makes zero-downtime deploys straightforward:

  1. Mark one server down in the upstream and reload Nginx (in-flight requests finish).
  2. Deploy to it and check its health endpoint directly.
  3. Put it back and repeat for the next server.

Database migrations must stay backward compatible while old and new versions run side by side; our zero-downtime deployment guide covers the expand/contract pattern.

The Load Balancer Is a Single Point of Failure

One load balancer means one server whose failure takes the site down. Options to remove it, in increasing complexity:

  • Accept it: a small, rarely changed Nginx server is very reliable, and you can rebuild it from configuration management in minutes.
  • Use DNS with several A records or a DNS failover service in front of two load balancers.
  • Use a floating IP with keepalived (VRRP) between two load balancers, if your provider supports moving an IP between servers.

ServoMix Plans for This Workload

A load balancer itself needs little: a 2 vCore / 8 GB server proxies a lot of traffic. Size the application servers for the per-server load, and give the database its own larger plan, since it is usually the first shared bottleneck.

AMD EPYC 7C13 VPS

Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
SMX vPro XS (AMD EPYC)28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹420/moWebsites, small APIs, bots, staging and VPN serversOrder
SMX vPro S (AMD EPYC)416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹560/moWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
SMX vPro M (AMD EPYC)832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹840/moProduction databases, high-traffic sites, multi-container appsOrder
SMX vPro L (AMD EPYC)1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹1,120/moLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

Intel Xeon Platinum 8269CY VPS

KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
SMX vCore XS (Intel Xeon)28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹270/moBusiness websites, small apps, scheduled jobs and botsOrder
SMX vCore S (Intel Xeon)416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
SMX vCore M (Intel Xeon)832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
SMX vCore L (Intel Xeon)1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,248/monthHigh-memory workloads and consolidated production serversCheck stock

Prices are monthly in INR as listed on the ServoMix store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.

Troubleshooting

SymptomCause and fix
Users randomly logged outSessions stored locally; move them to Redis or the database
One server gets most trafficLong-lived connections with round robin; switch to least_conn
All servers show the load balancer's IP as clientApp not reading X-Forwarded-For from the trusted LB IP
502 during deploysServers restarted while still in rotation; drain them first
Uploaded images missing intermittentlyFiles saved on a single server's disk; use shared storage

Frequently Asked Questions

Can Nginx be used as a load balancer?
Yes. The upstream module distributes requests across servers with round robin, least connections or hashing, and passive health checks skip failed servers.
Round robin or least_conn?
least_conn is usually the better default because request durations vary. Round robin works well when servers and requests are uniform.
Does open-source Nginx support active health checks?
No, active health checks are an NGINX Plus feature. Open-source Nginx uses passive checks (max_fails, fail_timeout); HAProxy offers active checks for free.
Where should HTTPS be terminated?
Usually on the load balancer, which holds the certificates. Traffic to app servers should be on a private network or encrypted if it crosses the public internet.
How do I handle sessions with a load balancer?
Store sessions centrally in Redis or the database so any server can handle any user. Sticky sessions (ip_hash) work but distribute load unevenly and break when a server fails.
When should I scale out instead of up?
When a single large server is near its limit, when you need redundancy against server failure, or when you want rolling deploys. Otherwise a bigger VPS is simpler.
Is the load balancer a single point of failure?
Yes, unless you add a second one with a floating IP (keepalived) or DNS failover. Many small setups accept the risk because a simple Nginx server rarely fails.
How many app servers do I need?
At least two for redundancy, so one can fail or be updated. Add more based on measured load per server, keeping enough capacity to handle traffic with one server down.
Do the app servers need public IPs?
They need to be reachable from the load balancer, ideally over a private network. Restrict their application ports to the load balancer's IP either way.
Which ServoMix plans suit a load-balanced setup?
A 2 vCore / 8 GB plan is plenty for the Nginx load balancer. Size app servers for their share of traffic, and give the database a larger plan, such as 8 vCores / 32 GB, since all app servers share it.

Frequently Asked Questions

Can Nginx be used as a load balancer?

Yes. The upstream module distributes requests across servers with round robin, least connections or hashing, and passive health checks skip failed servers.

Round robin or least_conn?

least_conn is usually the better default because request durations vary. Round robin works well when servers and requests are uniform.

Does open-source Nginx support active health checks?

No, active health checks are an NGINX Plus feature. Open-source Nginx uses passive checks ( max_fails , fail_timeout ); HAProxy offers active checks for free.

Where should HTTPS be terminated?

Usually on the load balancer, which holds the certificates. Traffic to app servers should be on a private network or encrypted if it crosses the public internet.

How do I handle sessions with a load balancer?

Store sessions centrally in Redis or the database so any server can handle any user. Sticky sessions ( ip_hash ) work but distribute load unevenly and break when a server fails.

When should I scale out instead of up?

When a single large server is near its limit, when you need redundancy against server failure, or when you want rolling deploys. Otherwise a bigger VPS is simpler.

Is the load balancer a single point of failure?

Yes, unless you add a second one with a floating IP (keepalived) or DNS failover. Many small setups accept the risk because a simple Nginx server rarely fails.

How many app servers do I need?

At least two for redundancy, so one can fail or be updated. Add more based on measured load per server, keeping enough capacity to handle traffic with one server down.

Do the app servers need public IPs?

They need to be reachable from the load balancer, ideally over a private network. Restrict their application ports to the load balancer's IP either way.

Which ServoMix plans suit a load-balanced setup?

A 2 vCore / 8 GB plan is plenty for the Nginx load balancer. Size app servers for their share of traffic, and give the database a larger plan, such as 8 vCores / 32 GB, since all app servers share it.

Was this article helpful?

Your feedback helps us continuously improve ServoMix guides.

Share this article

Know someone who may find this guide useful?

Nginx Load Balancer Across Multiple VPS Servers | ServoMix