Nginx is not just a high-performance web server; it’s also a powerful reverse proxy and load balancer. Instead of directing all user traffic to a single backend server, load balancing distributes the workload across a cluster of servers, ensuring high availability and better performance under heavy load.

This guide will walk you through the essential Nginx configuration steps to set up a robust load balancing environment using two or more backend servers.

Prerequisites

You should have a working Nginx installation and at least two backend application servers running.

  • Backend Server 1: 192.168.22.238
  • Backend Server 2: 192.168.22.239 (The new server we are adding)

The Core Concept: The upstream Module

To enable load balancing in Nginx, you must define a pool of backend servers using the upstream directive. This directive groups the servers and tells Nginx how to treat them as a single entity.

You will place the upstream block within the http block of your Nginx configuration file (e.g., /etc/nginx/nginx.conf or a file in /etc/nginx/conf.d/).

Step 1: Define the Upstream Server Group

Define your server group and give it a descriptive name, such as backend_servers.

Nginx

http {
    # --- UPSTREAM DEFINITION ---
    upstream backend_servers {
        server 192.168.22.238; # First Backend Server
        server 192.168.22.239; # Second Backend Server
        # Add more servers here if needed...
    }

    server {
        # ... Your server configuration will go here ...
    }
}

Step 2: Proxy Requests to the Upstream Group

Next, inside your server block and the target location block (location / in this case), you will modify the proxy_pass directive to point to the upstream group name instead of a fixed IP address.

The original configuration was:

Nginx

location / {
    proxy_pass http://192.168.22.238;
}

The new Load Balanced configuration is:

Nginx

server {
    listen 80;
    server_name example.com; # Your domain or IP

    location / {
        proxy_pass http://backend_servers; # Note: Pointing to the upstream name!
        
        # Optional: Standard best practices for reverse proxying
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Load Balancing Methods (Algorithms)

By default, Nginx uses the Round Robin algorithm, where requests are distributed sequentially across the servers. However, you can choose from several other strategies by adding a simple directive inside the upstream block:

AlgorithmNginx DirectiveDescriptionUse Case
Round Robin(Default)Requests are distributed equally in a rotating sequence.General purpose, stateless services.
Weightedserver IP **weight=N**;Requests are distributed based on a server’s defined capacity (weight). Higher weight gets more traffic.When backend servers have uneven processing power.
Least Connected**least_conn;**A request is routed to the server that has the fewest active connections.Long-running connections, highly variable request times.
IP Hash**ip_hash;**The server is chosen based on the client’s IP address. This ensures the same client is always sent to the same server.Necessary for maintaining Session Persistence (sticky sessions).

Example: Weighted Load Balancing

Use weight to send more traffic to a more powerful server (e.g., Server 1 gets 2/3 of the traffic):

Nginx

upstream backend_servers {
    server 192.168.22.238 weight=2;
    server 192.168.22.239 weight=1;
}

Example: Least Connected

Use least_conn for more intelligent distribution based on current workload:

Nginx

upstream backend_servers {
    least_conn;
    server 192.168.22.238;
    server 192.168.22.239;
}

Step 3: Test and Apply Configuration

After making changes to your Nginx configuration file:

  1. Test the syntax: Always run a test to catch any errors before applying:Bashsudo nginx -t
  2. Reload Nginx: If the test is successful, reload the service to apply the new load balancing configuration without dropping connections:Bashsudo nginx -s reload # OR sudo systemctl reload nginx

You have now successfully configured Nginx to load balance traffic across two (or more) backend servers, dramatically improving your application’s scalability and fault tolerance.

By Ray Lee (System Analyst)

iDempeire ERP Contributor, 經濟部中小企業處財務管理顧問 李寶瑞

Leave a Reply

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