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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
