How to Mirror Live Traffic with Nginx: Install, Configure, and Use ngx_http_mirror_module
This guide explains why copying production traffic to pre‑release environments is beneficial, walks through installing Nginx, details the ngx_http_mirror_module configuration for traffic mirroring, and provides custom build steps and useful monitoring commands.
Requirement
Copy production traffic to pre‑release or test environments to verify functionality, performance, avoid data generation, not affect real users, help troubleshoot, and test refactoring.
Install Nginx
Set up a yum repository file /etc/yum.repos.d/nginx.repo with the provided content, then run yum install nginx. The default configuration file is nginx.conf located in /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.
Start Nginx with nginx. Other commands: nginx -s stop, nginx -s quit, nginx -s reload, nginx -s reopen, and view processes with ps -ax | grep nginx.
ngx_http_mirror_module
The ngx_http_mirror_module (available since 1.13.4) creates background sub‑requests that mirror the original request; responses are ignored. It allows copying live traffic without affecting real users.
Example configuration:
location / {
mirror /mirror;
proxy_pass http://backend;
}
location = /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}To also mirror the request body:
location / {
mirror /mirror;
mirror_request_body off;
proxy_pass http://backend;
}
location = /mirror {
internal;
proxy_pass http://log_backend;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}Custom Build (if module not included)
Download source from http://nginx.org/en/download.html and compile with ./configure adding required options, ensuring --with-http_mirror_module is enabled, then make && make install.
Sample Configuration
upstream api.abc.com { server 127.0.0.1:8080; }
upstream tapi.abc.com { server 127.0.0.1:8081; }
server {
listen 80;
location /api {
proxy_pass http://api.cjs.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
mirror /newapi;
mirror /mirror2;
mirror /mirror3;
mirror_request_body on;
}
location /tapi {
proxy_pass http://tapi.cjs.com$request_uri;
proxy_pass_request_body on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Useful Commands
ps -eo pid,user,lstart,etime,cmd | grep nginx– view process runtime. netstat -an | grep ESTABLISHED | wc -l – count established connections. netstat -an | grep ":80" | wc -l – connections on port 80.
References
Official Nginx documentation: http://nginx.org/en/docs/ and the ngx_http_mirror_module page http://nginx.org/en/docs/http/ngx_http_mirror_module.html.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
