Nginx中將正式環境流量copy一份到測試環境

當測試環境需要一些數據時,除了仿造一些數據外,更方便和更好的做法是,將來自正式環境的用戶請求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域沒有給出,大家依照自己的配置即可。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章