Nginx Load Balancing

Beginner
6 min read
Updated: Nov 2026
4 examples
upstream

Definition

Load balancing spreads requests across several app servers. In Nginx you list backends in an upstream block and point proxy_pass at that group name.

Default method is round-robin. You can switch to least connections, IP hash (sticky-ish by client IP), and per-server weights.

Config

Start with this shape. Adjust paths for your server.

Nginx
http {
    upstream myapp1 {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://myapp1;
        }
    }
}

Use the upstream name after http:// in proxy_pass. No extra path is required for a full-site proxy.

Key Directives

NamePlain meaning
upstreamNamed group of backend servers.
least_connPrefer the backend with the fewest active connections.
ip_hashPick a backend from the client IP hash (session affinity).
weight=Send more traffic to stronger servers.
max_fails / fail_timeoutPassive health: skip a failing server for a while.

Examples

Example 1 — Default Round-Robin

With no method directive, each new request goes to the next server in turn.

Nginx
upstream myapp1 {
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

How It Works

  1. Equal servers share traffic evenly over time.
  2. proxy_pass http://myapp1; enables balancing.
  3. Works for HTTP and, with https://, for HTTPS backends.

Example 2 — Least Connections

Better when some requests are slow — busy servers get fewer new ones.

Nginx
upstream myapp1 {
    least_conn;
    server srv1.example.com;
    server srv2.example.com;
}

How It Works

  1. least_conn goes at the top of the upstream block.
  2. Long requests do not pile onto one box as easily.
  3. Still passive health checks apply on errors.

Example 3 — Sticky-ish Sessions with ip_hash

The same client IP tends to hit the same backend — useful for simple in-memory sessions.

Nginx
upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
}

How It Works

  1. Hash of client IP chooses the server.
  2. If that server is down, another is chosen.
  3. Not a full session store — shared Redis is still wiser for serious apps.

Example 4 — Weighted Servers

Give a stronger machine a higher weight so it receives more requests.

Nginx
upstream myapp1 {
    server srv1.example.com weight=3;
    server srv2.example.com;
    server srv3.example.com;
}
# roughly 3 of every 5 requests → srv1

How It Works

  1. Default weight is 1.
  2. weight=3 makes srv1 three times as likely in round-robin.
  3. Combine with least_conn or ip_hash in modern Nginx.

Frequently Asked Questions

No. proxy_pass http://127.0.0.1:3000; is enough for one app. upstream shines with two or more.
After max_fails errors within fail_timeout, Nginx avoids that server for a while, then probes again.
Yes — put servers in upstream and use fastcgi_pass http://mygroup; (same idea for uwsgi, etc.).
This runs inside Nginx on your machines. An ALB is a managed AWS service in front of instances.
No. Use ip_hash, sticky cookies (Plus), or an external session store.

Next: HTTPS servers

Keep going through the beginner Nginx path.

HTTPS servers →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful