Have you ever encountered a situation where, when your website traffic suddenly increases, a single server might slow down or even crash due to excessive load? In such cases, load balancing comes into play. Simply put, load balancing “distributes” user requests across multiple servers to prevent any single server from being overwhelmed, thereby improving the overall performance and stability of the system.

As a high-performance web server and reverse proxy tool, Nginx comes with a powerful load balancing module that can easily achieve multi-server traffic distribution. Next, we’ll guide you through configuring Nginx load balancing step by step in the simplest way possible.

I. Prerequisites for Load Balancing

Before starting the configuration, you need to prepare the following:

  1. At least two backend servers: These servers should run the same service (e.g., both have your web application deployed) and be able to respond to requests normally. Let’s assume we have two servers with IPs 192.168.1.100:8080 and 192.168.1.101:8080 (port numbers can be modified according to your actual setup).
  2. Nginx Installation: If Nginx isn’t installed on your server, refer to the official documentation for a quick install. For example, on Ubuntu:
   sudo apt update
   sudo apt install nginx

(For CentOS, use sudo yum install nginx; other systems, refer to the Nginx Official Installation Guide)
3. Ensure Backend Servers are Accessible: Check if the firewall on the backend servers allows access to the corresponding port (e.g., 8080), and verify you can access them directly via a browser (e.g., http://192.168.1.100:8080 should display your application page).

II. Core Configuration of Nginx Load Balancing

Nginx load balancing configuration involves two main steps: defining a backend server group and proxying requests to this group.

1. Define Backend Server Group (upstream Module)

In Nginx’s main configuration file (usually /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf), add an upstream block to list backend servers and set load balancing strategies:

http {
    # Define a backend server group (custom name, e.g., my_server_group)
    upstream my_server_group {
        # Simplest round-robin strategy: requests are distributed to backend servers in sequence
        server 192.168.1.100:8080;
        server 192.168.1.101:8080;

        # (Optional) Weighted Round-Robin: Higher weight means more requests
        # server 192.168.1.100:8080 weight=2;  # Weight 2: receives 2x more requests than the other
        # server 192.168.1.101:8080 weight=1;

        # (Optional) Health Check: Automatically skip failed backend servers
        # server 192.168.1.100:8080 max_fails=2 fail_timeout=10s;
        # max_fails: Number of allowed failures (default 1), marks as failed if exceeded
        # fail_timeout: Time to wait before retrying after failure (default 10s)
    }

    # Other Nginx configurations...
}

2. Configure Proxy to Backend Server Group

In the server block of Nginx, set up reverse proxy to forward user requests to the previously defined upstream group:

server {
    listen 80;  # Port Nginx listens on (usually 80 or 443)
    server_name localhost;  # Your domain name or server IP

    location / {
        proxy_pass http://my_server_group;  # Proxy to the backend server group
        proxy_set_header Host $host;        # Forward client request host
        proxy_set_header X-Real-IP $remote_addr;  # Forward client real IP
    }
}

Key Notes:
- proxy_pass: Must point to the upstream block name (e.g., http://my_server_group), not directly to the backend server IP.
- proxy_set_header: Forward client information to the backend server to prevent the backend from losing track of the real client IP and Host.

III. Verify Configuration Effectiveness

After configuration, verify as follows:

  1. Check Configuration Syntax: Run nginx -t in the terminal. If it shows syntax is ok and test is successful, the configuration syntax is correct.
  2. Reload Nginx: Execute nginx -s reload (or systemctl restart nginx) to apply the configuration.
  3. Test Load Balancing:
    - Open a browser and visit http://Nginx_server_IP (e.g., the public/private IP of your Nginx server).
    - Refresh the page multiple times and observe if the content comes from different backend servers (e.g., the backend server’s returned page may include its own IP or unique content).
    - Alternatively, check logs on the backend servers, e.g., tail -f /var/log/backend_server_logs, to confirm requests are being distributed correctly.

IV. Common Issues and Solutions

  1. Backend Server Unresponsive: Check if the backend server is running, the port is open, and the firewall isn’t blocking requests.
  2. Nginx Fails to Start: Use nginx -t to check for configuration errors, or view Nginx error logs (/var/log/nginx/error.log).
  3. All Requests Sent to One Server: Ensure the upstream block defines multiple servers and there are no syntax errors.
  4. Requests Still Sent to Failed Servers: Configure max_fails and fail_timeout (e.g., max_fails=2 fail_timeout=10s), and Nginx will automatically skip failed servers.

V. Advanced: Other Load Balancing Strategies

Beyond basic round-robin, Nginx supports the following strategies (choose as needed):

  • IP Hash: Distribute requests from the same client IP to the same backend server (suitable for session persistence):
  upstream my_server_group {
      ip_hash;  # Enable IP hash strategy
      server 192.168.1.100:8080;
      server 192.168.1.101:8080;
  }
  • URL Hash: Distribute requests based on the URL (requires installing the ngx_http_upstream_hash_module module).

By following the above steps, you’ve successfully implemented Nginx load balancing to distribute traffic across multiple servers. For further optimization, you can adjust weights, add HTTPS support, or configure more complex health checks to enhance system stability.

Xiaoye