nginx流量复制,nginx复制请求


一、需求:有多套测试环境,在与对接机构进行调试的时候,需要将某一环境的流量复制到其他服务器中。

二、方法:使用nginx的ngx_http_mirror_module模块。nginx 1.13.4及后续版本内置了ngx_http_mirror_module模块,提供流量镜像的复制功能。对于源站的请求,直接原路返回;源站把流量复制到mirror站,两者互不影响。

三、安装:
正常安装nginx1.13.4或者后续版本。下面安装nginx1.16.1版本。
1、下载nginx1.16.1源码 wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、安装依赖插件
yum -y install openssl openssl-devel
yum -y install pcre-devel
3、编译安装
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
make && make install

4、配置流量镜像复制,比较简单:
server {

    listen       10000;
    root /data0/;

    #源站
    location / {
        #开发环境
        mirror /mirror1; 
        
        #sit环境
        mirror /mirror2;
       
        #UAT环境
        mirror /mirror3;
       
        #灰度环境
        proxy_pass http://192.168.100.10:10000; 
        mirror_request_body on;     
     }  
     
     #镜像站点
     location /mirror1 {
        internal;
        proxy_pass http://192.168.100.20:20000$request_uri;
        proxy_pass_request_body on;
        
        proxy_set_header X-Original-URI $request_uri;
     }  
     
     location /mirror2 {
        internal;
        proxy_pass http://192.168.100.30:30000$request_uri;
        proxy_pass_request_body on;
      
        proxy_set_header X-Original-URI $request_uri;
     }  
     
     location /mirror3 {
        internal;
        proxy_pass http://192.168.100.40:40000$request_uri;
        proxy_pass_request_body on;
        
        proxy_set_header X-Original-URI $request_uri;
     }  
     

}

以上配置复制get和post请求,如果不需要复制post请求,则设置源站 mirror_request_body off和镜像站点proxy_pass_request_body off。
增加一份mirror,则可以把流量增大一倍,如:

#开发环境
mirror /mirror1;
mirror /mirror1;

相关内容