ngx_http_mirror_module(nginx 1.13.4开始支持),实现了创建原始请求的镜像,可以实现流量复制的效果。
在业务开发的流程里,针对特定业务,需要评估硬件资源能承载的并发数、吞吐量从而评估系统的服务质量。
这个时候可以用ngx_http_mirror_module 模块来实现流量复制,上手简单,易配置。
image.png
下载nginx 1.14版本:
# wget http://nginx.org/download/nginx-1.14.2.tar.gz //下载nginx
# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz //下载lua-nginx-module模块
#yum -y install pcre-devel lua
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--with-http_perl_module --with-http_gzip_static_module \
--with-http_stub_status_module \
--with-poll_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--add-module=/nginx/lua-nginx-module-0.10.13 //nginx解压之后,编译安装
# make && make install
mirror的配置参考如下:
upstream test_backend {
server 47.102.126.49:80;
}
server {
listen 81;
server_name report.abc.com;
access_log logs/report.abc.com_81_access.log xluser;
error_log logs/report.abc.com_81_error.log;
location =/cgi-bin/report.fcg {
mirror /mirror; //重点配置
client_body_buffer_size 10m;
keepalive_timeout 0;
access_log /usr/local/nginx/logs/report.abc.com_report_81_info.log ;
error_log /usr/local/nginx/logs/report.abc.com_report_81_info.log ;
chunked_transfer_encoding off;
#lua_need_request_body on;
#content_by_lua_file 'conf/lua/ipfilter.lua'; //自己写的ipfilter模块,需要lua支持
#lua_code_cache on;
proxy_pass http://localhost:3005;
}
location /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}
}
官方文档:
https://nginx.org/en/docs/http/ngx_http_mirror_module.html
https://github.com/nginx/nginx/blob/master/src/http/modules/ngx_http_mirror_module.c