Nginx流量复制

生产环境出现的Bug在测试环境无法复现(即使是数据同步,代码同步也未排查出来),所以打算做个流量复制。

流量复制模块:ngx_http_mirror_module

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 8282;
        server_name  _;
        access_log  /var/log/nginx/access.log;
        location / {
            index index.html;
            root /usr/share/nginx/html;
        }
    }

    server{
    	listen 8181;
   		server_name localhost;
    	access_log /var/log/nginx/mirror_access.log;
	}
    
    upstream back {
        server 127.0.0.1:8282;
    }
    upstream mirror1 {
        server 127.0.0.1:8181;
    }
    server {
        listen       80;
        server_name  _;

        location / {
            mirror /mirror1;
            proxy_pass http://back;
	}

        location = /mirror1 {
            proxy_pass http://mirror1$request_uri;
        }

        include /etc/nginx/default.d/*.conf;

    }

}

配置说明

location / {                        # location /指定了源uri为/,也可以定义为其他指定接口
    mirror /mirror1;                # mirror /mirror指定镜像uri为/mirror,有多个需要复制流量的,可以配置多条
    mirror /mirror1;                # 配置多条情况下,将会起到流量放大的作用,即主配置请求一次,镜像端会有两次
  # mirror_request_body on;         # 指定是否镜像请求body部分,请求自动缓存;
    proxy_pass http://back;      # 指定处理主流量的后端Server
}
location = /mirror1 {
    internal;       # 指定此location只能被“内部的”请求调用,外部的调用请求会返回”Not found” (404)
    proxy_pass http://mirror1$request_uri;    # 指定将要复制流量的Server1
}

 

posted @ 2021-11-04 17:05  MegaloBox  阅读(398)  评论(0编辑  收藏  举报