Master Nginx Load Balancing: Algorithms, Configurations & Best Practices

This article explains how Nginx performs load balancing, describes its key algorithms—including round robin, weighted round robin, IP hash, and least connections—shows configuration examples, and highlights benefits such as improved availability, scalability, and security for large‑scale web architectures.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master Nginx Load Balancing: Algorithms, Configurations & Best Practices

Nginx load balancing distributes client requests across multiple backend servers (e.g., application1, application2 … applicationN), reducing load and improving application availability and performance.

By horizontally scaling, Nginx can add or remove backend servers to handle traffic changes, enabling dynamic expansion. It also serves as the first line of defense, filtering and distributing traffic to help mitigate attacks.

Load balancing prevents a single point of failure, enhancing system reliability and availability.

Load Balancing Algorithms

Nginx offers several load‑balancing algorithms, commonly used ones include round robin (default), weighted round robin, IP hash, and least connections.

Round Robin (default)

The default algorithm distributes requests sequentially to each backend server.

Operation principle:

The load balancer maintains a list of backend servers and cycles through it.

When a new request arrives, the next server in the list is selected.

After reaching the end of the list, it returns to the start.

Configuration example:

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

    server {
        listen 80;
        location / {
            proxy_pass http://myapp;
            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;
        }
    }
}

Round robin suits most basic load‑balancing needs; for more complex scenarios, weighted round robin or least connections can be used.

Weighted Round Robin

Weighted round robin assigns a weight to each backend server; higher weight servers receive more requests.

Configuration example:

http {
    upstream myapp {
        server backend1.example.com weight=3;
        server backend2.example.com weight=2;
        server backend3.example.com weight=1;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://myapp;
            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;
        }
    }
}

This algorithm is useful when backend servers have different processing capacities.

IP Hash

IP hash uses the hash of the client’s IP address to route requests, providing session persistence.

Configuration example:

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

    server {
        listen 80;
        location / {
            proxy_pass http://myapp;
            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;
        }
    }
}

Suitable for applications that require session affinity, such as shopping carts or payment systems. Health checks are essential to handle server failures.

Least Connections

Least connections directs new requests to the backend server with the fewest active connections.

Configuration example:

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

    server {
        listen 80;
        location / {
            proxy_pass http://myapp;
            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;
        }
    }
}

This algorithm helps avoid overloading any single server, adapting to varying request processing times.

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.

Backend DevelopmentAlgorithms
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.