Kivi

没有什么远大理想,只是永远都不会满足而已


  • 首页

  • 关于

  • 标签

  • 归档

nginx流量复制

发表于 2017-08-06 分类于 nginx 阅读次数:
本文字数: 4.4k 阅读时长 ≈ 4 分钟

参考连接

通过Nginx拷贝请求流量到测试环境测试

原理

nginx没有内置模块可以实现流量复制的功能,需要借助lua脚本实现流量复制的功能

依赖安装和下载脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 首先download需要的nginx脚本,然后放到/opt/目录下(命名为nginx),并创建lib文件夹

cd /opt/nginx/lib

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc8.tar.gz
mv v0.10.9rc8.tar.gz lua-nginx-module-v0.10.9rc8.tar.gz

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
mv v0.3.0.tar.gz ngx_devel_kit-v0.3.0.tar.gz

wget https://github.com/openresty/headers-more-nginx-module/archive/v0.32.tar.gz
mv v0.32.tar.gz headers-more-nginx-module-v0.32.tar.gz

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/opt/nginx/lib/pcre-8.38
make
make install

wget https://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/opt/nginx/lib/zlib-1.2.11
make
make install

wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar zxvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make
make install PREFIX=/opt/nginx/lib/LuaJIT-2.0.5
export LUAJIT_LIB=/opt/nginx/lib/LuaJIT-2.0.5/lib
export LUAJIT_INC=/opt/nginx/lib/LuaJIT-2.0.5/include/luajit-2.0

cd /opt/nginx/lib
tar zxvf lua-nginx-module-v0.10.9rc8.tar.gz
tar zxvf ngx_devel_kit-v0.3.0.tar.gz
tar zxvf headers-more-nginx-module-v0.32.tar.gz

cd /opt/nginx
./configure \
--user=nginx --group=nginx --prefix=/opt/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_addition_module \
--with-pcre=/opt/nginx/lib/pcre-8.38 \
--with-zlib=/opt/nginx/lib/zlib-1.2.11 \
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
--add-module=/opt/nginx/lib/ngx_devel_kit-0.3.0 \
--add-module=/opt/nginx/lib/headers-more-nginx-module-0.32 \
--add-module=/opt/nginx/lib/lua-nginx-module-0.10.9rc8
make

lua脚本

创建文件: /opt/nginx/conf/lua/copy_req.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
local res1, res2, action
action = ngx.var.request_method
if action == "POST" then
arry = {method = ngx.HTTP_POST, body = ngx.req.read_body()}
else
arry = {method = ngx.HTTP_GET}
end

if ngx.var.svr == "on" then
res1, res2 = ngx.location.capture_multi {
{ "/product" .. ngx.var.request_uri , arry},
{ "/test" .. ngx.var.request_uri , arry},
}
else
res1, res2 = ngx.location.capture_multi {
{ "/product" .. ngx.var.request_uri , arry},
}
end

if res1.status == ngx.HTTP_OK then
local header_list = {"Content-Length", "Content-Type", "Content-Encoding", "Accept-Ranges", "set-cookie"}
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
-- ngx.say(res1.body)
ngx.exit(ngx.HTTP_NOT_FOUND) -- 这里跟原文有点改动,我使用的1.10.0,需要这么写才能正常返回404页面
end

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
http {

# 其他配置

upstream product {
server 127.0.0.1:8001;
}
upstream test {
server 127.0.0.1:8002;
}

server {
listen 8000;
server_name test;

location ~* ^/product {
log_subrequest on;
rewrite ^/product(.*)$ $1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://product;
access_log logs/product-upstream.log;
}

location ~* ^/test {
log_subrequest on;
rewrite ^/test(.*)$ $1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://test;
access_log logs/test-upstream.log;
}

location ~* ^/(.*)$ {
set $svr "on"; #开启或关闭copy功能
content_by_lua_file "conf/lua/copy_req.lua";
}

error_page 404 /404.html;
location = /404.html {
root html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

}

启动

启动nginx,访问8000端口,负责响应请求的是8001端口的服务,所有打到8001端口的服务的post请求也会打到8002端口的服务上,这就通过nginx实现了流量复制的功能。上面只是demo,实际使用中可以根据实际条件调整·

# nginx
mongodb生产部署建议
Linux的CPU,内存,磁盘IO,网络压力测试方法
  • 文章目录
  • 站点概览
kivi

kivi

nodejs | server
58 日志
17 分类
32 标签
RSS
  1. 1. 参考连接
  2. 2. 原理
  3. 3. 依赖安装和下载脚本
  4. 4. lua脚本
  5. 5. nginx配置
  6. 6. 启动
© 2019 kivi | 173k | 2:37
由 Hexo 强力驱动 v3.9.0
|
主题 – NexT.Pisces v7.3.0
|