Nginx流量copy到测试环境的方法

from net

测试环境中需要一些数据,可以自己仿造数据,不过最方便的方法是:将来自正式环境的用户请求copy一份到测试环境。
依赖模块:
lua-nginx-module,ngx_devel_kit, headers-more-nginx-module
以上模块在github上均可以找到,作者是国内nginx的著名开发者agentzh。
使用模块需要重新编译
复制代码代码示例:
nginx, –add-module=/path/to/your/module
nginx配置:

复制代码代码示例:

>>cat copy_req.lua
local res1, res2, action
action = ngx.var.request_method
if action == “POST” then
arry = {method = ngx.HTTP_POST, body = request_body}
else
arry = {method = ngx.HTTP_GET}
end
if ngx.var.svr == “on” then
res1, res2 = ngx.location.capture_multi {
{ “/s1″ .. ngx.var.request_uri , arry},
{ “/test” .. ngx.var.request_uri , arry},
}
else
res1, res2 = ngx.location.capture_multi {
{ “/s1″ .. ngx.var.request_uri , arry},
}
end
if res1.status == ngx.HTTP_OK then
local header_list = {“Content-Length”, “Content-Type”, “Content-Encoding”, “Accept-Ranges”}
for _, i in ipairs(header_list) do
if res1.header[i] then
ngx.header[i] = res1.header[i]
end
end
ngx.say(res1.body)
else
ngx.status = ngx.HTTP_NOT_FOUND
end
>>cat   nginx.conf
……..
location ~* ^/s1 {
log_subrequest on;
rewrite ^/s1(.*)$ $1 break;
proxy_pass http://s1;
access_log /opt/logs/nginx/youni/upstream.log;
}
location ~* ^/test {
log_subrequest on;
rewrite ^/test(.*)$ $1 break;
proxy_pass http://test;
access_log /opt/logs/nginx/youni/upstream.log;
}
location ~* ^/(.*)$ {
client_body_buffer_size 2m;
set $svr     “on”;               #开启或关闭copy功能
content_by_lua_file    req_fenliu.lua;
}

upstream s1 {
server  x.x.x.x;
}
upstream test {
server  xx.xx.xx.xx;
}
……

其中的nginx.conf内的http,server域没有放上来,大家根据自己的实际情况配置即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值