Master Nginx Load Balancing: Algorithms, Reverse Proxy, and Real‑World Configs
This article explains how Nginx functions as a load balancer and reverse proxy, detailing its event‑driven architecture, multi‑process model, and core configuration, and compares common algorithms such as round‑robin, least‑connections, IP‑hash, weighted round‑robin, and weighted least‑connections with practical code examples.
Nginx Load Balancing
Nginx can act as a load balancer, distributing requests to multiple backend servers to improve performance, reliability, and scalability. It supports various algorithms such as round‑robin, least‑connections, IP‑hash, weighted round‑robin, and weighted least‑connections.
1. Working Principle
Nginx achieves high performance through an event‑driven, non‑blocking architecture that leverages the OS’s I/O multiplexing, an event loop that processes events without blocking, and a multi‑process model where each worker process runs its own event loop.
Key configuration: worker_processes defines the number of worker processes.
2. Reverse Proxy
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 the backend service at http://localhost:8080, while hiding the real server details.
3. Load‑Balancing Algorithms
Example application (Tornado) used for testing:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, Tornado!")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
print("Server running on http://localhost:8888")
tornado.ioloop.IOLoop.current().start()Round‑Robin
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;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.62.0/24;
deny all;
}
}Least 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;
}
}IP Hash
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;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.62.0/24;
deny all;
}
}Weighted Round‑Robin
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;
}
}Weighted Least Connections
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;
}
}4. Summary
Reverse proxy acts as a bridge between clients and backend servers, providing security and load‑balancing capabilities. By intelligently distributing requests, Nginx ensures balanced load across servers, improves overall performance, and maintains stability under high concurrency.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
