Ngx-http-split-clients modules separate client connections based on specific conditions (e.g. IP address, request headers, cookies, etc.)
Sample configuration:
HTTP {
split-clients "${remote-addr}aaa" $variant {
0.5%. One;
2.0%. Two;
- "";
}
server {
location/{
index index${variant}.html;
You can use the $cookie-... To detach the request as a source, the source string is hashed using CRC32 and the hash percentage is the value of the source.
Instructions
Syntax: Split-clients $variable {...}
Default value: None
Working with Fields: HTTP
Discovery Module Official website wiki to the above example configuration code has several problems, I compile the installation, according to the Wiki method configuration nginx.conf 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 discovery of these errors was due to the fact that Nginx had REMOTE_ADDR variables and no remote-addr.
Then I say a little bit of knowledge of the Split clients module, my own time test out ~
On 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";
To allow us to test the results.
Split Clients module module is to cut the client IP and then use CRC32 to calculate a value to match ·
In the Russian web site to translate such a paragraph:
This directive creates a/b split-Change
Test, for example:
HTTP {
split_clients "${remote_addr}aaa" $variant {
0.5%. One;
2%. Two;
* "";
}
The value of the original string variable is a hash
Using the CRC32. In this example, when
Hash value from 0 to 21474836 (0.5%), variable $ variant
has a value. " One ". If the hash value is 21474837
To 107374182 (2%)-". Two ". And if the hash value from 107374183
4294967297-"".
That is to say, my IP address is 192.168.1.29 server IP for 192.168.1.28
When I visit Nginx, Nginx will cut my IP address to match. 1
Log:
Copy Code code as follows:
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 "
See the page is thr
When I modify my IP for 192.168.220.29 server IP for 192.168.220.28
Looking at the log:
Copy Code code as follows:
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 "
See the page is two
PS: This kind of painting nginx $variant variable can bring us a variety of benefits to determine from which IP segment to which server ~!