Mirroring Incoming Web Traffic With Nginx

In this guide , We will see how to mirror incoming HTTP Web traffic to another upstream or location block without affecting the Original traffic.

What Is Nginx?

Nginx is an open source , high performance web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

What Is Nginx Mirroring?

A new mirroring module was introduced with the release of Nginx 1.13.

This modules allows us to mirror the incoming original HTTP Web requests to any backend servers.

By doing so , We can avoid recreating entire infrastructure.

All we have to do is , Just add few lines of code to Nginx configuration and we are ready to mirror the requests.

PreRequisites

  • Make sure You are using Nginx with v1.13 or later.
  • A server to which requests will be mirrored.

Flow Diagram

The above diagram illustrates that the requests coming to the NGINX server (having Nginx module configured) will send a copy of the requests to the MIRROR NODE.

The above setup doesn’t impact the functionality or the performance of the web server.

For example:

  • A requests comes to the example.com (NGINX server) it Proxy pass to application server and the flow works as usual.
  • Also The requests will be sent to example1.com (MIRROR NODE) , Also called as Another Nginx web server.

Lets go ahead and implement the same.

Mirroring Web Traffic Using Nginx

Configuring mirror will the Nginx mirror directive can be implemented inside server or location block.

Login to NGINX server.

Lets setup upstream servers where the requests should be mirrored.

Go to the directory where Nginx configuration files are stored.

In my case , It is /etc/nginx/sites-available and the file I will be modifying is example.conf

Open the configuration file and add the upstream as shown below.

The upstream should be added before the server block.

upstream backend_mirrors {
    server example1.com
}

Next step is to modify the location block in order to mirror all the requests from that location block.

We have to add a mirror directive (mirror /mirror;) and mirror_request_body directive (mirror_request_body on;) to the location block in order to mirror all the requests.

location / {
   mirror /mirror;
   mirror_request_body on;
   root /usr/share/nginx/html;
   index index.html index.htm;
}

If you have multiple location blocks within a Nginx configuration file , You can add the below lines to the location block.

mirror /mirror;
mirror_request_body on;

We need to restrict the mirrored requests to be accessible from the internet.By doing so, the External users wont be able to access /mirror directly from the internet.

location /mirror {
   internal;
   proxy_pass http://backend_mirrors$request_uri;
   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;
}

All the requests will be sent only to the backend servers.

After the above configurations are applied , Save and close the file and run the below command.

The command is to verify whether Nginx configuration is valid and Syntax is correct.

nginx -t

You should get the below response.

And then we need to restart the nginx service for the changes to take effect.

service nginx restart

After it is done , Any request to example.com will be mirrored to example1.com

We can verify the same by sending the test traffic to example.com and check the nginx access logs on Both the servers.

Hope you found it helpful.

Please do check out other blogs.