Record the Split Clients module configuration process for the Nginx server

  • 2020-05-10 23:30:05
  • OfStack

The ngx-http-split-clients module separates client connections based on 1 specific criteria (e.g., ip address, request header,cookies, etc.)
Example configuration:


http {
  split-clients "${remote-addr}AAA" $variant {
    0.5% .one;
    2.0% .two;
    - "";
  }
 
  server {
    location / {
       index index${variant}.html;

$cookie-... To separate the request as a source, the source string is hashed using CRC32 and the hash percentage will be the value of the source.
instruction


split-clients

Syntax: split-clients $variable {... }
Default: none
Use field: http

I found several problems with the sample configuration code above given by wiki on the official website of the module. After compiling and installing, I configured nginx.conf according to the method of wiki to report an error.

My actual code is:


http {
  split_clients "${remote_addr}AAA" $variant {
    0.5% .one;
    2% .two;
    3% .eric;
    4% .yang;
    50% .thr;
    * "";
  }

  server {
    location / {
       index index${variant}.html;
  }

Then create a few new files


cd /usr/local/nginx/html/

echo "one" >index.one.html
echo "two" >index.two.html
echo "eric" >index.eric.html
echo "thr" >index.thr.html

Configuration differences:


wiki : split-clients   eric:split_clients
wiki : remote-addr    eric: remote_addr
wiki : - "";      eric: * "";
The reason for finding these errors is that nginx has remote_addr variable and remote-addr variable
Then I will talk about the 1 bit of knowledge of Split Clients module, which I have tested on my own time
For the test, we output the ${variant} variable on the nginx error log

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
         '$status $body_bytes_sent "$http_referer" '
         '"$http_user_agent" "$http_x_forwarded_for" "$variant"';
So that we can test the results.
The module of Split Clients is to cut the client IP and then use CRC32 to calculate a value to match ·
Here's a translation on a Russian website:
This directive creates an A/B split 1 variable
Tests, such as:

http {
  split_clients "${remote_addr}AAA" $variant {
    0.5% .one;
    2% .two;
    * "";
  }

The original value of the string variable was a hash
Using CRC32. In this case, when
Hash value from 0 to 21474836 (0.5%), variable $variant
Have the value ". 1 ". If the hash is 21474837
To 107374182 (2%) - ". Two ". And if you go from 107374183 hash
4294967297 - "".
That is to say, my IP address is 192.168.1.29 and server IP is 192.168.1.28
When I visit nginx, nginx cuts my IP address to match to.1
Log:


192.168.1.29 - - [01/Apr/2011:15:39:17 +0800] "GET / HTTP/1.1" 403 571 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" ".thr"

The page you see is thr
When I modified my IP to 192.168.220.29 server IP to 192.168.220.28
Looking at the log:


192.168.220.29 - - [01/Apr/2011:15:44:46 +0800] "GET / HTTP/1.1" 403 571 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" ".two"

The page you see is two
PS: the $variant variable in nginx can bring us all kinds of benefits.


Related articles: