Linux
Web-Server
Nginx
Nginx as Lb

Nginx as Load Balancer Configuration for Ubuntu

Prerequisites

  • Installed Nginx on your Ubuntu server
  • Root or sudo access to the server
  • Multiple backend servers to distribute traffic

Step 1: Install Nginx

If Nginx is not installed, install it using the following command:

sudo apt update && sudo apt install nginx -y

Step 2: Configure Nginx as a Load Balancer

Edit the Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

How Nginx Load Balancing Works

Nginx distributes incoming client requests across multiple backend servers based on predefined algorithms. This helps in:

  • Improving performance by distributing the load.
  • Enhancing fault tolerance by rerouting traffic if one server fails.
  • Optimizing resource utilization to prevent overloading any single server.

Sample Configuration:

http {
    upstream backend {
        server backend1.example.com:80;
        server backend2.example.com:80;
        server backend3.example.com:80;
    }
 
    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;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

Step 3: Test and Restart Nginx

Check the configuration syntax:

sudo nginx -t

Restart Nginx to apply changes:

sudo systemctl restart nginx

Step 4: Verify Load Balancing

Use curl or a browser to access the load balancer:

curl -I http://example.com

This should forward requests to different backend servers.

Load Balancing Algorithms

Nginx supports different load balancing algorithms:

Round Robin (Default)

Each request is forwarded to the next server in the list sequentially.

upstream backend {
    server backend1.example.com:80;
    server backend2.example.com:80;
}

Least Connections

Requests are sent to the server with the fewest active connections.

upstream backend {
    least_conn;
    server backend1.example.com:80;
    server backend2.example.com:80;
}

Weighted Load Balancing

You can distribute traffic in a 40-60 ratio, for example:

upstream backend {
    server backend1.example.com:80 weight=4;
    server backend2.example.com:80 weight=6;
}

This means 40% of traffic will be directed to backend1, and 60% of traffic to backend2.

IP Hash (Sticky Sessions)

This ensures that a particular client always reaches the same backend server.

upstream backend {
    ip_hash;
    server backend1.example.com:80;
    server backend2.example.com:80;
}

Logs & Debugging

Check Nginx logs for debugging issues:

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Conclusion

Nginx is a powerful and lightweight load balancer that efficiently distributes traffic among multiple backend servers. You can customize load balancing strategies to match your performance and availability requirements.


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!