Master Nginx Load Balancing: Round‑Robin, Weighted, IP‑Hash & Least‑Conn Explained

This guide walks through Nginx’s four primary load‑balancing methods—round‑robin, weighted, IP‑hash, and least‑connections—explaining their principles, when to use each, and providing complete configuration examples and code snippets to help you distribute traffic efficiently across multiple backend servers.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master Nginx Load Balancing: Round‑Robin, Weighted, IP‑Hash & Least‑Conn Explained

Nginx is a high‑performance HTTP and reverse‑proxy server that can also act as a load balancer.

It can distribute traffic among multiple backend servers, improving availability, scalability, and performance.

Load‑balancing algorithms

The common algorithms include round‑robin, weighted, IP‑hash, and least‑connections, each suited to different scenarios.

1. Round‑robin

The default strategy assigns requests to servers in a fixed order, ideal when backend servers have similar capacity.

Configuration example:

http {
    upstream mikechen {
        server backend1.mikechen.cc;
        server backend2.mikechen.cc;
        server backend3.mikechen.cc;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://mikechen;
        }
    }
}

2. Weighted

Weighted load balancing lets you assign different weights to servers so that more powerful servers receive more requests.

Example weights:

server backend1.mikechen.cc weight=5;
server backend2.mikechen.cc weight=3;
server backend3.mikechen.cc weight=1;

3. IP‑hash

IP‑hash hashes the client’s IP address to consistently route a user to the same backend, useful for session‑affinity.

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

    server {
        listen 80;
        server_name example.com;

        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;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

4. Least‑connections

The least‑connections method sends new requests to the server with the fewest active connections, helping balance uneven request processing times.

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

    server {
        listen 80;
        server_name example.com;

        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;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

These configurations enable efficient traffic distribution and can be chosen based on server capacity, session requirements, or request latency characteristics.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendload balancingConfigurationNGINXWeb server
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.