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_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段的分到哪個服務器上~!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章