当前位置:Gxlcms > PHP教程 > Nginx拷贝流量

Nginx拷贝流量

时间:2021-07-01 10:21:17 帮助过:18人阅读

利用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

end

8. POST参数中有中文,编码问题:

Nginx配置UTF8 : 在server节点下配置 charset utf-8;

tomcat配置URL编码格式:


以上就介绍了Nginx拷贝流量,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行