SoFunction
Updated on 2025-04-12

How to forward traffic proportionally in Nginx

Nginx can usesplit_clientsInstructions or passweightParameters and Lua scripts implement traffic forwarding in proportion. The following are two specific operation steps and example configurations:

Method 1: Use the split_clients command

1. Configure split_clients

split_clientsThe instruction can allocate traffic to different backend server groups in a set proportion based on the hash value of the client IP address. The basic syntax is:

split_clients $variable {
    percentage1 backend1;
    percentage2 backend2;
    ...
    default backend_default;
}

in,$variableIt is usually the client IP address (such as$remote_addr);percentageFor the allocation ratio, the range is 0 - 100%;backendis a backend server group.

2. Configure the backend server group

useupstreamDirectives define backend server groups.

3. Configure the virtual host

In the virtual host configuration,split_clientsThe result of the request is forwarded to the corresponding backend server group.

Sample configuration

Suppose you want to forward traffic to two backend server groups at 70% and 30% respectivelybackend1andbackend2, you can refer to the following configuration:

# Distribute traffic proportionallysplit_clients $remote_addr {
    70% backend1;
    30% backend2;
}

# Define the backend server groupupstream backend1 {
    server :80;
}

upstream backend2 {
    server :80;
}

# Virtual Host Configurationserver {
    listen 80;
    server_name ;

    location / {
        # Forward requests based on the result of split_clients        proxy_pass http://$split_clients;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Configuration instructions

  • split_clients: According to the hash value of the client IP address, 70% of the traffic is directedbackend1, 30% traffic orientationbackend2
  • upstream: Define two backend server groupsbackend1andbackend2
  • server: Virtual host configuration, listen to port 80, and request according tosplit_clientsThe results are forwarded to the corresponding backend server group.

Application configuration

After the configuration is complete, the Nginx configuration needs to be reloaded for the changes to take effect:

sudo nginx -s reload

Through the above steps, you can use Nginx to forward traffic proportionally.

Method 2: Pass the weight parameter and Lua script (weighted polling)

In addition to usingsplit_clientsIn addition to the instructions, Nginx can also passweightParameters and Lua scripts implement traffic forwarding in proportion:

Use weight parameter

weightParameters can be used forupstreamIn the block, traffic is allocated proportionally by setting the weights of different servers.

Sample configuration

# Define the backend server groupupstream backend {
    # Weight is 7, indicating that approximately 70% of traffic is received    server :80 weight=7; 
    # Weight is 3, indicating that approximately 30% of traffic is received    server :80 weight=3; 
}

# Virtual Host Configurationserver {
    listen 80;
    server_name ;

    location / {
        # Forward request to the backend server group        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;
    }
}

Configuration instructions

  • upstreamTwo backend servers are defined in the blockand, set the weights to 7 and 3 respectively. Nginx will direct approximately 70% of traffic based on the weight ratio., 30% traffic orientation
  • serverThe block is a virtual host configuration, listens to port 80 and forwards the request tobackendThis backend server group.

Using Lua scripts

With Lua scripts, you can implement more complex traffic allocation logic. Need to make sure Nginx is installedngx_http_lua_moduleModule.

Sample configuration

# Define the backend server groupupstream backend1 {
    server :80;
}

upstream backend2 {
    server :80;
}

# Virtual Host Configurationserver {
    listen 80;
    server_name ;

    location / {
        # Use Lua scripts to allocate traffic        access_by_lua_block {
            (())
            local random_num = (1, 10)
            if random_num <= 7 then
                .proxy_pass = "http://backend1"
            else
                .proxy_pass = "http://backend2"
            end
        }
        # Forward request        proxy_pass $proxy_pass;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Configuration instructions

  • Define two backend server groupsbackend1andbackend2
  • existlocationUse in blocksaccess_by_lua_blockExecute Lua script. The script generates a random number between 1 and 10. If the random number is less than or equal to 7, the request will be forwarded tobackend1; otherwise forward tobackend2, thereby achieving approximately 70% and 30% of traffic allocation. After the configuration is complete, reload the Nginx configuration using the following command:
sudo nginx -s reload

These methods have their own advantages and disadvantages, and you can choose according to specific needs and scenarios.

This is the article about how Nginx forwards traffic proportionally. For more related content related to Nginx traffic proportionally forwarding, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!