Nginx can usesplit_clients
Instructions or passweight
Parameters 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_clients
The 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,$variable
It is usually the client IP address (such as$remote_addr
);percentage
For the allocation ratio, the range is 0 - 100%;backend
is a backend server group.
2. Configure the backend server group
useupstream
Directives define backend server groups.
3. Configure the virtual host
In the virtual host configuration,split_clients
The 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% respectivelybackend1
andbackend2
, 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 groupsbackend1
andbackend2
。 -
server
: Virtual host configuration, listen to port 80, and request according tosplit_clients
The 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_clients
In addition to the instructions, Nginx can also passweight
Parameters and Lua scripts implement traffic forwarding in proportion:
Use weight parameter
weight
Parameters can be used forupstream
In 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
-
upstream
Two 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
。
-
server
The block is a virtual host configuration, listens to port 80 and forwards the request tobackend
This 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_module
Module.
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 groups
backend1
andbackend2
。 - exist
location
Use in blocksaccess_by_lua_block
Execute 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!