Nginx拷貝流量
阿新 • • 發佈:2019-02-15
利用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. 安裝LuaJIT
# cd /data/nginxinstall
# wget http://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=/usr/local/lib:$LD_LIBRARY_PATH
4. 下載準備httpLuaModule
# cd /data/nginxinstall
# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
# tar -xzvf v0.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;
}
upstream test {
server 10.10.10.103:80;
}
在server結點下新增
location ~* ^/antispam {
client_body_buffer_size 2m;
set $svr "on"; #開啟或關閉copy功能
content_by_lua_file copy_flow.lua;
}
location ~* ^/online {
log_subrequest on;
rewrite ^/s1(.*)$ $1 break;
proxy_pass http://s1; -- 反向代理,跳轉到upstream online
}
location ~* ^/test {
log_subrequest on;
rewrite ^/test(.*)$ $1 break;
proxy_pass http://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需要引數傳遞
arry = {method = ngx.HTTP_POST, body = ngx.var.request_body, args=arg}
else
arry = {method = ngx.HTTP_GET}
end
if ngx.var.svr == "on" then
online, test = ngx.location.capture_multi {
{ "/online" .. ngx.var.request_uri, arry},
{ "/test" .. ngx.var.request_uri, arry},
}
else
online = ngx.location.capture_multi {
{ "/online" .. ngx.var.request_uri, arry}
}
end
if online.status == ngx.HTTP_OK then -- 只返回online server的結果
ngx.say(online.body)
else
ngx.status = ngx.HTTP_NOT_FOUND
開發環境: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. 安裝LuaJIT
# cd /data/nginxinstall
# wget http://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=/usr/local/lib:$LD_LIBRARY_PATH
4. 下載準備httpLuaModule
# cd /data/nginxinstall
# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
# tar -xzvf v0.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;
}
upstream test {
server 10.10.10.103:80;
}
在server結點下新增
location ~* ^/antispam {
client_body_buffer_size 2m;
set $svr "on"; #開啟或關閉copy功能
content_by_lua_file copy_flow.lua;
}
location ~* ^/online {
log_subrequest on;
rewrite ^/s1(.*)$ $1 break;
proxy_pass http://s1; -- 反向代理,跳轉到upstream online
}
location ~* ^/test {
log_subrequest on;
rewrite ^/test(.*)$ $1 break;
proxy_pass http://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需要引數傳遞
arry = {method = ngx.HTTP_POST, body = ngx.var.request_body, args=arg}
else
arry = {method = ngx.HTTP_GET}
end
if ngx.var.svr == "on" then
online, test = ngx.location.capture_multi {
{ "/online" .. ngx.var.request_uri, arry},
{ "/test" .. ngx.var.request_uri, arry},
}
else
online = ngx.location.capture_multi {
{ "/online" .. ngx.var.request_uri, arry}
}
end
if online.status == ngx.HTTP_OK then -- 只返回online server的結果
ngx.say(online.body)
else
ngx.status = ngx.HTTP_NOT_FOUND