利用Ngnix將線上流量拷貝到測試機(一個http請求視為一份流量)開發環境:CentOS 6.4nginx安裝目錄:/usr/local/nginx-1.4.2各模組安裝包下載目錄: /data/nginxinstall1. 安裝pcre #yum -y install pcre pcre-devel2. 安裝zlibyum -y install zlib zlib-devel3. 安裝zlibyum -y install zlib zlib-devel3. 安裝LuaJIT //luajit.org/download/LuaJIT-2.0.2.tar.gz# tar -xzvf LuaJIT-2.0.2.tar.gz# cd LuaJIT-2.0.2# makelib和include是直接放在/usr/local/lib和usr/local/include配置環境變數export LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0/export :$LD_LIBRARY_PATH4. 下載準備httpLuaModule# cd /data/nginxinstall# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.6.gtarxdvf . .8.65. 下面開始安裝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 install6. 編輯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, actionaction = ngx.var.request_methodif 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}endif ngx.var.svr == "on" thenonline, test = ngx.lo. { "/online" .. ngx.var.request_uri, arry}, { "/test" .. ngx.var.request_uri, arry},}else online online" .. ngx.var.request_uri, arry} }endif 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_FOUNDend8. POST參數中有中文,編碼問題:格式: 以上就介紹了Nginx拷貝流量,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。