ngx-http-split-clients模塊基于一些特定條件分開客戶端連接,(例如ip地址,請求頭,cookies等)
示例配置:
http { split-clients "${remote-addr}AAA" $variant { 0.5% .one; 2.0% .two; - ""; } server { location / { index index${variant}.html;
可以使用$cookie-…作為來源來分離請求,來源字符串使用CRC32進行哈希計算并且哈希百分比將作為來源的值。
指令
split-clients
語法:split-clients $variable { … }
默認值:none
使用字段:http
發現模塊官網wiki給的上面的示例配置代碼有幾點問題,我編譯安裝后,按照wiki的方法配置nginx.conf 報錯。
我實際的代碼是:
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.htmlecho "two" >index.two.htmlecho "eric" >index.eric.htmlecho "thr" >index.thr.html
配置差別:
wiki : split-clients eric:split_clientswiki : remote-addr eric: remote_addrwiki : - ""; eric: * "";關于這些錯誤的發現是因為 nginx 有 remote_addr 變量 并沒有 remote-addr ·我就順藤摸瓜·
隨后我來講下 Split Clients模塊的一點點知識,我自己時間測試出來的~
關于測試,我們在 nginx 的錯誤日志上 輸出 ${variant} 變量
log_format main '$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 - - [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"
看到的頁面是 thr
當我修改我的 IP 為 192.168.220.29 服務器IP 為 192.168.220.28
在看日志:
復制代碼代碼如下:
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"
看到的頁面是 two
PS:這樣的畫 nginx 里的$variant 變量 可以給我們帶來各種好處了·判斷來自哪個IP段的分到哪個服務器上~!