NGINX: Send a copy of requests to another upstream

Solution 1:

After some searhing in nginx docs I found a directory mirror in nginx.org. Think, this is what u need.

We are using kubernetes with nginx as ingress, so our config is

      location /api/what-i-want-to-mirror {
          set $proxy_upstream_name "gateway-api-service-svc-80";
          proxy_pass http://upstream_balancer;
          mirror /mirror;

          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

      location = /mirror {
          set $proxy_upstream_name "integration-gateway-api-service-svc-80";
          proxy_pass http://upstream_balancer;
          internal;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_set_header X-Original-URI $request_uri;
      }

We've done this as ASAP but it works like a charm.