跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

Nginx拷贝流量

利用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. 安装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# makelib和include是直接放在/usr/local/lib和usr/local/include配置环境变量export LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH4. 下载准备httpLuaModule# cd /data/nginxinstall# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz# tar -xzvf v0.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;}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, actionaction = ngx.var.request_methodif action == "POST" thenngx.req.read_body() -- 解析 body 参数之前一定要先读取 bodylocal arg = ngx.req.get_post_args() -- post需要参数传递arry = {method = ngx.HTTP_POST, body = ngx.var.request_body, args=arg}elsearry = {method = ngx.HTTP_GET}endif ngx.var.svr == "on" thenonline, test = ngx.location.capture_multi {{ "/online" .. ngx.var.request_uri, arry},{ "/test" .. ngx.var.request_uri, arry},}elseonline = ngx.location.capture_multi {{ "/online" .. ngx.var.request_uri, arry}}endif online.status == ngx.HTTP_OK then -- 只返回online server的结果ngx.say(online.body)elsengx.status = ngx.HTTP_NOT_FOUND end

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

Nginx配置UTF8 : 在server节点下配置 charset utf-8;Nginx在宝塔面板中轻松管理Nginx高性能Web服务器。提供可视化配置反向代理、负载均衡、SSL证书及HTTP缓存功能,一键优化高并发性能,助您高效搭建稳定、快速的网站运行环境。

下载tomcat配置URL编码格式:以上就介绍了Nginx拷贝流量,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关文章