Nginx拷贝流量
来源:互联网 发布:在北京干什么挣钱知乎 编辑:程序博客网 时间:2024/06/14 12:12
利用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
end
8. POST参数中有中文,编码问题:
Nginx配置UTF8 : 在server节点下配置 charset utf-8;
tomcat配置URL编码格式: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
9. test 出现延迟,从而Lua模拟出的线程出现延迟,影响线上, 需要添加如下参数:
location ~* ^/test {
proxy_connect_timeout 100ms;
proxy_read_timeout 100ms;
proxy_send_timeout 100ms;
proxy_pass http://test;
}
1 0
- Nginx拷贝流量
- Nginx流量控制
- Nginx流量拦截算法
- nginx 小流量架构
- nginx定时开关切流量
- nginx lua 限制流量脚步
- nginx大流量负载调优
- nginx/tengine限制流量如何配置
- nginx常用的流量分离技术
- Nginx学习之如何防止流量攻击
- Prometheus 监控 Nginx 流量 (三)
- php+nginx高并发大流量站点nginx优化
- 使用tcpcopy拷贝线上流量压测测试环境
- nginx图片服务器优化 解决流量和存储问题
- nginx 防垃圾流量的perl清洗脚本
- Nginx配置文件(反向代理+动态流量控制)
- nagios监控mysql主机,nginx,磁盘IO,网卡流量
- 为高流量站点优化NGINX和PHP-fpm配置
- android客户端和php服务简单交互
- 【Leetcode】:22. Generate Parentheses 问题 in Go语言
- 递归 - 移梵塔
- Xcode : Your session has expired. Please log in
- HDU 1002 A + B Problem II -- java大法好
- Nginx拷贝流量
- readelf --help
- 数据流重定向
- PHP搭建自己的web框架-视图/模板引擎
- PHP实现的mysql数据库操作类
- 使用maven将项目打成jar包
- ListView从入门到中级,android菜鸟级别,
- Missing iOS Distribution signing identity for
- 回溯算法之集装箱问题