Using Nginx ngx_http_mirror_module for Traffic Mirroring and Testing
This article explains why and how to copy production traffic to pre‑release or test environments using Nginx's ngx_http_mirror_module, covering repository setup, installation, basic start/stop commands, configuration examples, and custom compilation for full module support.
The need for traffic mirroring is to verify functionality and performance with real requests, troubleshoot live issues, and safely test refactorings without affecting actual users.
To achieve this, Nginx provides the ngx_http_mirror_module which creates background mirror sub‑requests while ignoring their responses.
1. Install Nginx
Create a YUM repository file /etc/yum.repos.d/nginx.repo with the following content:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=trueInstall with:
yum install nginx -yThe default configuration file is nginx.conf , typically located in /etc/nginx or /usr/local/nginx/conf . Start, stop, reload, and query processes using:
# start nginx
nginx
# fast shutdown
nginx -s stop
# graceful shutdown
nginx -s quit
# reload configuration
nginx -s reload
# reopen log files
nginx -s reopen
# list processes
ps -ax | grep nginx2. Mirror Module Configuration
The module works like a mirror site that records all original requests. A simple example from the official docs:
location / {
mirror /mirror;
proxy_pass http://backend;
}
location = /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}To disable request‑body mirroring, add mirror_request_body off; . To enable it, set mirror_request_body on; and adjust headers as needed.
3. Custom Build (if the binary lacks the module)
Download the source from nginx.org and compile with the desired modules:
./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--without-http_limit_req_module \
--without-http_mirror_module \
--with-pcre=../pcre-8.43 \
--with-zlib=../zlib-1.2.11 \
--add-module=/path/to/ngx_devel_kit \
--add-module=/path/to/lua-nginx-module
make && make install4. Example Full 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;
# traffic mirroring
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;
}
}Additional useful commands for monitoring Nginx processes and connections are provided, such as ps -eo pid,user,lstart,etime,cmd | grep nginx and netstat -an | grep ESTABLISHED | wc -l .
References to official Nginx documentation and third‑party resources (LuaJIT, OpenResty) are listed for further reading.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.