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