Nginx 镜像模块

mirror 将主请求复制到内部镜像地址,镜像响应不影响客户端。适合流量采样和灰度验证,不要镜像支付、写库等非幂等请求。

流量镜像

upstream production {
    server 127.0.0.1:3000;
}

upstream staging {
    server 127.0.0.1:3001;
}

server {
    listen 80;
    server_name example.com;

    location / {
        mirror /_mirror;
        mirror_request_body on;
        proxy_pass http://production;
    }

    location = /_mirror {
        internal;
        proxy_pass http://staging$request_uri;
        proxy_set_header Host $host;
    }
}

验证

curl -X POST http://127.0.0.1/api/test -d 'dry-run=1'
tail -n 20 /var/log/nginx/error.log

注意