With the release of Nginx 1.13, a new mirroring module became available that allows you to mirror traffic to an additional backend. A scenario where this feature would be beneficial is analytics. Rather than rearchitecting your infrastructure, you can add a few lines of configuration to Nginx.
This tutorial will guide you throw enabling the module and then mirroring traffic to a backend server.
Scenerios for Mirroring
The are several use cases for wanting to mirror your traffic.
Risk-Free Performance and Quality Assurance Testing
No amount of testing can prepare you for the onslaught of a production environment, no matter how creative you believe you are. A user will simply out-smart you in how to break an application.
Eliminate risk by mirroring requests to a backend QAT environment.
Application Monitoring
Application monitoring, for example, can eat up a lot of precious resources. Rather than waste them by installing agents on all of your production application hosts, you could dedicate a server or a cluster of servers to mirror all requests to.
Analytics
Similar to application monitoring, capturing analytics usually requires having to install agents on your production servers. Remove the burden from those servers and place them elsewhere. By mirroring all requests to your Analytics backend, you can capture the same data without affecting production servers.
File Uploads
You may have a WordPress blog powered by multiple backend hosts. Mirroring could be used to ensure all file uploads are uploaded to all hosts at the same time.
Creating a Mirror
Create a group of backend servers we’ll mirror all requests to.
upstream backend_mirrors { server host1.example.com }
Modify the location you want to mirror all requests. Add the new mirror directive. If you would like to mirror the entire request body, add the new mirror_request_body directive, too.
location / { mirror /mirror; mirror_request_body on; root /usr/share/nginx/html; index index.html index.html }
Set up a mirror that accepts internal access only. This prevents others from
being able to access /mirror directly from the Internet. The requested URI will be sent to the mirror.
location /mirror { internal; proxy_pass https://backend_mirrors; proxy_set_header X-SERVER-PORT $server_port; proxy_set_header X-SERVER-ADDR $server_addr; proxy_set_header HOST $http_host; proxy_set_header X-REAL-IP $remote_addr; }
Restart Nginx to apply your changes.