Nginx Config by Example: Load Balancing
Distributing traffic across multiple servers with this code example showing upstream block definition for backend server groups, load balancing methods including round-robin and least connections, server weight configuration, and health check parameters.
Code
http {
# Define upstream server group
upstream backend {
# Load balancing method: least_conn
least_conn;
# Backend servers with weights
server backend1.example.com:8080 weight=3;
server backend2.example.com:8080 weight=2;
server backend3.example.com:8080;
# Backup server (used when others fail)
server backup.example.com:8080 backup;
# Health check parameters
server backend4.example.com:8080 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
}
}Explanation
Nginx acts as a software load balancer using the upstream module to define groups of backend servers, distributing client requests across multiple servers to prevent overload and improve performance and scalability. The upstream block lists backend servers by IP address or hostname and port, with Nginx automatically distributing requests among them based on the selected load balancing algorithm.
Load balancing methods available in Nginx include:
- Round Robin (default) distributes requests sequentially to backend servers
least_connsends requests to the server with fewest active connectionsip_hashensures requests from same client IP go to same backend serverhashwith custom key enables consistent hashing based on variables- Weighted distribution using
weightparameter controls traffic proportion
Server parameters provide fine-grained control: weight assigns proportional traffic distribution, max_fails sets the number of failed connection attempts before marking a server unavailable, fail_timeout defines how long a server remains unavailable after failures, and backup designates servers that only receive traffic when primary servers are down. Health checking happens passively through actual client requests rather than active probing.
Code Breakdown
upstream backend defines server group named backend.least_conn load balancing method routes to server with fewest connections.weight=3 assigns proportional traffic, this server gets 3x traffic.max_fails=3 fail_timeout=30s health check parameters for passive monitoring.
