WIT LAB INC Blog

“ To Impress Others By Works"

Setting Up Nginx as a Load Balancer

Setting Up Nginx as a Load Balancer

What is Load Balancing?

Load balancing is a technique used to distribute incoming traffic across multiple backend servers to optimize performance, prevent overload, and ensure high availability. Nginx, as a powerful reverse proxy and load balancer, helps distribute requests efficiently among multiple backend servers.

Why Use Nginx for Load Balancing?

✅ High Availability – Keeps applications running even if one server fails.
✅ Scalability – Easily handles growing traffic by adding more backend servers.
✅ Better Performance – Reduces server overload and improves response times.
✅ Fault Tolerance – Ensures continuous operation by rerouting traffic when servers fail.

Step-by-Step Nginx Load Balancer Setup

Step 1: Install Nginx

First, install Nginx on the server that will act as the load balancer.
For Ubuntu/Debian:

sudo apt update
sudo apt install nginx -y

For CentOS/RHEL:

sudo yum install epel-release -y
sudo yum install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 2: Configure Nginx for Load Balancing

Open the Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Add the following load balancing configuration:

http {
    upstream backend_servers {
        server 192.168.1.101;
        server 192.168.1.102;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend_servers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

Explanation:

  • upstream backend_servers – Defines backend servers that will handle requests.
  • server 192.168.1.101; server 192.168.1.102; – Specifies backend servers.
  • proxy_pass http://backend_servers; – Sends client requests to backend server

Step 3: Test Nginx Configuration

Before restarting Nginx, check if the configuration is correct:

sudo nginx -t

If successful, restart Nginx:

sudo systemctl restart nginx

Step 4: Verify Load Balancing

To test whether load balancing is working, run:

while true; do curl http://example.com; sleep 1; done

This will repeatedly send requests, allowing you to verify traffic distribution.

Load Balancing Algorithms in Nginx

Round Robin (Default)

  • Requests are distributed sequentially among servers.
  • Ideal for environments where servers have equal capacity.
  • Example: If there are three servers (A, B, C), requests are distributed as A → B → C → A → B → C.

Least Connections

  • Sends requests to the server with the fewest active connections.
  • Useful for applications where request processing time varies.

IP Hash

  • Route requests based on the client’s IP address.
  • Ensures that a user always connects to the same backend server.
  • Useful for session-based applications.

Weighted Load Balancing

  • Assigns different weights to servers based on capacity.
  • Example: If Server A is more powerful than Server B, assign A a weight of 3 and B a weight of 1.
upstream backend_servers {
    server 192.168.1.101 weight=3;
    server 192.168.1.102 weight=1;
}

Conclusion

Nginx is a powerful load balancer that enhances scalability, reliability, and performance. With different load balancing algorithms, businesses can optimize their application’s efficiency based on specific requirements. By implementing reverse proxy, SSL, caching, and sticky sessions, applications become highly available and secure.

Page Top