How to Use Nginx as an HTTP Load Balancer in Linux
Introduction
When deploying multiple application servers for redundancy, load balancing distributes incoming requests across backend servers. This gives you increased availability through redundancy, enhanced reliability, and improved scalability — additional servers can be added as traffic grows.
Nginx functions as an efficient HTTP load balancer, distributing network traffic among application servers and returning responses to clients.
Load balancing methods supported by Nginx
- Round-robin — distributes requests to servers sequentially; the default when no method is specified.
- Least-connected — routes the next request to the server with the fewest active connections.
- IP-hash — uses a hash function based on the client IP address to determine server selection, enabling session persistence by binding clients to specific servers.
Advanced configurations use server weights to influence the balancing algorithm. Nginx also supports health checks, marking servers as failed for a configurable period (default: 10 seconds) if responses fail, so they temporarily stop receiving requests.
Testing environment setup
Load Balancer: 192.168.18.7
Application server 1: 192.168.28.1
Application server 2: 192.168.28.2
Both application instances are accessed via the domain example.com. Add this DNS A
record:
A Record @ 192.168.18.7
Or, for local testing, add this to your /etc/hosts file:
192.168.18.7 example.com
Setting up Nginx load balancing
Installation
$ sudo apt install nginx [On Debian/Ubuntu]
$ sudo yum install nginx [On CentOS/RHEL]
Configuration
Create a server block file at /etc/nginx/conf.d/loadbalancer.conf:
$ sudo vi /etc/nginx/conf.d/loadbalancer.conf
Add this basic round-robin configuration:
upstream backend {
server 192.168.28.1;
server 192.168.28.2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://backend;
}
}
proxy_pass passes requests to the servers referenced in the upstream block. Requests
distribute between servers using weighted round-robin balancing by default.
Alternative load balancing methods
Least connection method:
upstream backend {
least_conn;
server 192.168.28.1;
server 192.168.28.2;
}
IP hash session persistence:
upstream backend {
ip_hash;
server 192.168.28.1;
server 192.168.28.2;
}
Server weights:
upstream backend {
server 192.168.28.1 weight=4;
server 192.168.28.2;
}
With this configuration, of six requests, 192.168.28.1 receives 4 and 192.168.28.2
receives 2.
Validation and restart
$ sudo nginx -t
$ sudo systemctl restart nginx
$ sudo systemctl enable nginx
Testing Nginx load balancing
Open a browser and navigate to http://example.com. Observe which server instance
loads, then refresh repeatedly — the application should eventually load from the second
server, confirming load balancing is working.