首頁 >後端開發 >php教程 >Nginx拷貝流量

Nginx拷貝流量

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-07-29 08:51:211363瀏覽
利用Ngnix將線上流量拷貝到測試機(一個http請求視為一份流量)
開發環境:CentOS 6.4
nginx安裝目錄:/usr/local/nginx-1.4.2
各模組安裝包下載目錄: /data/nginxinstall
1. 安裝pcre
 #yum -y install pcre pcre-devel
2. 安裝zlib
yum -y install zlib zlib-devel
3. 安裝zlib
yum -y install zlib zlib-devel
3. 安裝LuaJIT
//luajit.org/download/LuaJIT-2.0.2.tar.gz
# tar -xzvf LuaJIT-2.0.2.tar.gz
# cd LuaJIT-2.0.2
# make
lib和include是直接放在/usr/local/lib和usr/local/include
配置環境變數
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
/export :$LD_LIBRARY_PATH
4. 下載準備httpLuaModule
# cd /data/nginxinstall
# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.6.gtarxdvf . .8.6
5. 下面開始安裝Nginx(嘗試採用nginx1.10.0遇到編譯有錯誤):
# cd /data/nginxinstall/
# wget http://nginx.org/download/nginx-1.4.2.tar .gz 
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=. ./lua-nginx-module-0.8.6
# make -j2
# make install
6. 編輯nginx/conf/nginx.conf檔(本檔案是json格式,可以看做一棵樹):
在http結點下方新增:
    upstream online {
         server  10.10.12.7:80;
    }
 
    }
在server結點下加上
location ~* ^/antispam {
client_body_buffer_size 2m;
    set $svr     "on";               #開啟或關閉copy功能
    content_by_lua_file  online log_subrequest on;
     rewrite ^/s1(.*)$ $1 break;
     proxy_pass http://s1; -- 反向代理,跳到upstream online
}
location ~* ^/test {
    log_subrequest on;
   rewrite ^/test(break*) 1/test; //test; -- 反向代理,跳到upstream test
}
7. 編輯nginx/copy_flow.lua 檔案:
local online, test, action
action = ngx.var.request_method
if action == "POST " then
    ngx.req.read_body() -- 解析body 參數之前一定要先讀body
    local arg = ngx.req.get_post_args() -- post需要參數傳遞
. = ngx.var.request_body, args=arg}
else
    arry = {method = ngx.HTTP_GET}
end
if ngx.var.svr == "on" then
online, test = ngx.lo. { "/online" .. ngx.var.request_uri, arry},
   { "/test" .. ngx.var.request_uri, arry},
}
else
    online online" .. ngx.var.request_uri, arry}
    }
end
if online.status == ngx.HTTP_OK then -- 只回傳online server的結果
ngx.say(online.body)
else -- 只回傳online server的結果
ngx.say(online.body)
else
ngx.status = ngx.HTTP_NOT_FOUND



end

8. POST參數中有中文,編碼問題:

格式:

以上就介紹了Nginx拷貝流量,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn