How to Set Up a Load Balancer for High Availability
Load balancers distribute incoming traffic across multiple servers, preventing any single server from becoming overwhelmed. This ensures high availability, better performance, and fault tolerance. Whether you’re using a hardware appliance or software like Nginx or HAProxy, the core setup follows similar steps.
In this tutorial, we’ll walk through setting up a basic software load balancer using Nginx on Ubuntu. We assume you have at least two backend servers running your application and one dedicated server for the load balancer.
1. Install and Configure Nginx
SSH into your load balancer server and install Nginx:
sudo apt update
sudo apt install nginx
Open the default config file:
sudo nano /etc/nginx/nginx.conf
Find the http block and add an upstream directive listing your backend servers:
upstream backend {
server 192.168.1.10:80;
server 192.168.1.11:80;
}
2. Set Up the Server Block
Inside the same http block, configure a server block to proxy requests:
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;
}
}
Save the file and test the configuration:
sudo nginx -t
sudo systemctl restart nginx
3. Enable Load Balancing Methods
Nginx defaults to round-robin. For session persistence (sticky sessions), add ip_hash inside the upstream block:
upstream backend {
ip_hash;
server 192.168.1.10:80;
server 192.168.1.11:80;
}
4. Health Checks and Monitoring
For basic checks, use passive health monitoring (Nginx marks a server down after a failure). For active checks, consider using HAProxy or a third-party module. You can also monitor logs:
sudo tail -f /var/log/nginx/access.log
Conclusion
Setting up a load balancer with Nginx takes only a few minutes. By distributing traffic across multiple servers, you gain reliability and scalability. For production, add SSL termination and fine-tune timeouts. Experiment with different algorithms to find what works best for your application.