Master Nginx Load Balancing: Algorithms, Configs, and Real‑World Examples
This article explains how Nginx functions as a load balancer, covering its event‑driven architecture, reverse‑proxy setup, and five common balancing algorithms—round‑robin, least connections, IP hash, weighted round‑robin, and weighted least connections—along with detailed configuration examples and performance metrics.
What Is Nginx Load Balancing?
Nginx can act as a load balancer by distributing incoming requests to multiple backend servers, improving performance, reliability, and scalability. It supports several balancing algorithms such as round‑robin, least connections, IP hash, weighted round‑robin, and weighted least connections.
How Nginx Works
Event‑driven architecture : Nginx uses a non‑blocking, event‑driven model that leverages the operating system’s multiplexing mechanisms to handle many concurrent connections with minimal thread switching.
Event loop : A single‑threaded loop waits for events, processes them when they occur, and then returns to waiting, maximizing CPU utilization.
Multi‑process model : The worker_processes directive spawns multiple independent processes, each with its own event loop, allowing Nginx to use multi‑core CPUs and avoid single‑point failures.
Reverse Proxy Configuration
server {
listen 80;
server_name example.com;
root /root/build/;
index index.html;
location /api/server/ {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}This configuration forwards requests matching /api/server/ to a backend service running on port 8080, while serving static files from /root/build/. The proxy_set_header directives preserve the original host and client IP.
Load‑Balancing Algorithms
1. Round‑Robin
The default algorithm distributes requests sequentially across servers, similar to a queue.
upstream tornado_servers {
server 192.168.31.158:8888;
server localhost:8888;
}
server {
listen 80;
server_name 192.168.62.132;
location / {
proxy_pass http://tornado_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}2. Least Connections
Requests are sent to the server with the fewest active connections.
upstream tornado_servers {
least_conn;
server 192.168.31.158:8888;
server localhost:8888;
}
server {
listen 80;
server_name 192.168.62.132;
location / {
proxy_pass http://tornado_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}3. IP Hash
Clients are consistently routed to the same backend based on a hash of their IP address, preserving session affinity.
upstream tornado_servers {
ip_hash;
server 192.168.31.158:8888;
server localhost:8888;
}
server {
listen 80;
server_name 192.168.62.132;
location / {
proxy_pass http://tornado_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}4. Weighted Round‑Robin
Each server receives a weight; higher‑weight servers handle more requests.
upstream tornado_servers {
server 192.168.31.158:8888 weight=5;
server localhost:8888 weight=3;
}
server {
listen 80;
server_name 192.168.62.132;
location / {
proxy_pass http://tornado_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}5. Weighted Least Connections
Combines connection count and weight to choose the optimal server.
upstream tornado_servers {
least_conn;
server 192.168.31.158:8888 weight=5;
server localhost:8888 weight=3;
}
server {
listen 80;
server_name 192.168.62.132;
location / {
proxy_pass http://tornado_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Monitoring Nginx Status
Enabling stub_status provides metrics such as accepts (total connections accepted), handled (connections successfully processed), requests (total client requests), and the current Reading, Writing, and Waiting counts.
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.62.0/24;
deny all;
}Conclusion
By placing Nginx as a reverse proxy and configuring appropriate load‑balancing algorithms, you can hide backend details, improve security, and distribute traffic efficiently across multiple servers. Proper monitoring and algorithm selection ensure high concurrency handling and stable system performance under load.
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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
