nginx流量复制与放大

时间:2022-07-22
本文章向大家介绍nginx流量复制与放大,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

应用场景

  • 复制线上流量至测试环境,模拟线上环境
  • 流量放大,做压测

使用方式

# cat mirror.conf 
server {
    listen       9999;
    server_name  172.20.111.180;
    access_log /export/server/nginx/logs/mirror.log main;
    error_log /export/server/nginx/logs/mirror_error.log  error;

    #设置源,即要请求的目标
    location / {
        mirror /mirror;
        mirror_request_body on; 
        proxy_pass http://172.20.110.221:8080;
        }

    #复制源请求
    location = /mirror {
        internal;
        proxy_pass http://172.20.111.48:8080$request_uri;
    }
}

其中

mirror_request_body on  为默认开启,表示是否镜像请求body部分,与 proxy_request_buffering、fastcgi_request_buffering、scgi_request_buffering 和 uwsgi_request_buffering 冲突,一旦开启 mirror_request_body 为 on,则请求自动缓存。

流量放大也是非常的简单,只需要在请求源的时候多复制一份即可

server {
     listen 80;
     server_name _;
     
    location / {
        mirror /mirror;
        mirror /mirror;
        mirror_request_body on; 
        proxy_pass http://127.0.0.1:8080;
        }
     location = /mirror {
        internal;
        proxy_pass http://127.0.0.1:8080$request_uri;
     }

这样既去请求时,请求一次,日志里便会有两次记录。

# curl 127.0.0.1/index.html
查看请求便会有两次记录
172.20.111.180 - - [16/Jul/2019:19:09:08 +0800] "GET /index.html HTTP/1.0" 200 612 "-" "curl/7.29.0" "-"
172.20.111.180 - - [16/Jul/2019:19:09:08 +0800] "GET /index.html HTTP/1.0" 200 612 "-" "curl/7.29.0" "-"

官方文档参考:http://nginx.org/en/docs/http/ngx_http_mirror_module.html