How to Set Up a Load Balancer: A Step-by-Step Guide

Load balancing distributes incoming traffic across multiple servers to improve performance, reliability, and scalability. Whether using hardware or software like NGINX or HAProxy, a proper setup ensures your application stays available under heavy load. This guide walks through the essential steps for configuring a basic load balancer using NGINX as an example.

Before you begin, you need at least two backend servers running the same application (e.g., web servers on ports 3000 and 3001) and one load balancer server. All servers should have a static IP address and be on the same network. For this tutorial, we assume Ubuntu 22.04 with NGINX installed.

Article illustration

1. Install and Configure NGINX as a Load Balancer

On the load balancer machine, install NGINX:

  • sudo apt update && sudo apt install nginx -y
  • Open the default config: sudo nano /etc/nginx/sites-available/default
  • Replace the contents with a basic upstream block pointing to your backend servers.

Example Upstream Configuration

Inside the http block, add:

upstream backend {
    server 192.168.1.10:3000;
    server 192.168.1.11:3001;
}

Then in the server block, proxy requests to the upstream group:

location / {
    proxy_pass http://backend;
}

2. Choose a Load Balancing Method

NGINX supports several algorithms. By default, it uses round-robin. To switch, add a directive inside the upstream block:

  • Least Connections: least_conn; – sends traffic to the server with fewest active connections.
  • IP Hash: ip_hash; – ensures a client always reaches the same backend (useful for sessions).
  • Weighted: Add weight=3 after a server IP to give it more traffic.

3. Enable Health Checks (Optional but Recommended)

NGINX Plus has built-in health checks; for open source, you can rely on proxy_next_upstream to retry failed requests. Add inside the location block:

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

Alternatively, use a third-party module like nginx_upstream_check_module.

4. Test and Start the Load Balancer

Check the configuration for syntax errors:

sudo nginx -t

If successful, restart NGINX:

sudo systemctl restart nginx

Now access the load balancer’s IP address in a browser. Traffic should be distributed across your backend servers.

Conclusion

Setting up a load balancer with NGINX is straightforward and provides immediate benefits in reliability and performance. Start with a simple round-robin, then add health checks and session persistence as your application grows. Monitor your server logs to verify traffic distribution and adjust weights or algorithms as needed.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *