nginx的Split Clients模块
在nginx 官网上浏览这些日子nginx 的更新,看到 更新了Split Clients 的模块·然后就看了一下·发现官网wiki的几点问题·写帮助文档的人有些不负责,我编译安装后,按照wiki的方法配置nginx.conf 报错。官网wiki :
http://wiki.nginx.org/HttpSplitClientsModule
http {
split-clients "${remote-addr}AAA" $variant {
0.5% .one;
2.0% .two;
- "";
}
server {
location / {
index index${variant}.html;
我实际的代码是:
http {
split_clients "${remote_addr}AAA" $variant {
0.5% .one;
2% .two;
3% .eric;
4% .yang;
50% .thr;
* "";
}
server {
location / {
index index${variant}.html;
}
然后新建几个文件
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
配置差别:
wiki : split-clients eric:split_clients
wiki : remote-addr eric: remote_addr
wiki :- ""; eric: * "";
关于这些错误的发现是因为 nginx 有 remote_addr 变量 并没有 remote-addr ·我就顺藤摸瓜·
随后我来讲下 Split Clients模块的一点点知识,我自己时间测试出来的~
关于测试,我们在 nginx 的错误日志上 输出 ${variant} 变量
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$variant"';
以便于我们测试结果。
Split Clients 的模块 模块 是切割 客户端IP 然后然后 使用CRC32的 算出一个值去匹配·
在 俄文网站上 翻译出这么一段:
该指令创造了A / B分割一变
测试,例如:
http {
split_clients "${remote_addr}AAA" $variant {
0.5% .one;
2% .two;
* "";
}
原来的字符串变量的值是哈希
使用CRC32的。在这个例子中,当
哈希值从0到21474836(0.5%),变量$变种
有值“。之一”。如果哈希值21474837
至107374182(2%) - “。两个”。而如果从107374183哈希值
4294967297 - “”。
也就是说,比如 我的IP地址是 192.168.1.29 服务器IP 为 192.168.1.28
当我访问 nginx 的时候,nginx 会切割我的IP地址 匹配到 .1
日志:
192.168.1.29 - - "GET / HTTP/1.1" 403 571 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" ".thr"
看到的页面是 thr
当我修改我的 IP 为 192.168.220.29 服务器IP 为 192.168.220.28
在看日志:
192.168.220.29 - - "GET / HTTP/1.1" 403 571 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" ".two"
看到的页面是 two
PS:这样的画 nginx 里的$variant 变量 可以给我们带来各种好处了·判断来自哪个IP段的分到哪个服务器上~!
页:
[1]