記錄Nginx服務器的Split Clients模塊配置過程
更新時間:2016年01月27日 14:29:28 作者:Deidara
這篇文章主要介紹了Nginx服務器的Split Clients模塊的配置過程記錄,ngx-http-split-clients模塊用于切分客戶端連接,需要的朋友可以參考下
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
發(fā)現(xiàn)模塊官網(wǎng)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.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: * "";關于這些錯誤的發(fā)現(xiàn)是因為 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的 算出一個值去匹配·
在 俄文網(wǎng)站上 翻譯出這么一段:
該指令創(chuàng)造了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段的分到哪個服務器上~!
您可能感興趣的文章:
相關文章
Nginx?Rewrit實現(xiàn)網(wǎng)頁跳轉功能詳細步驟
Rewrite主要實現(xiàn)url地址重寫,以及重定向,就是把傳入web的請求重定向到其他url的過程,這篇文章主要介紹了Nginx?Rewrit實現(xiàn)網(wǎng)頁跳轉功能詳細步驟,需要的朋友可以參考下2024-02-02