nginx request duplication

Imagine if you could duplicate a request to nginx and thus send it to two different servers. This could be great if e.g. one wanted to inspect the contents of an inbound HTTP POST request, with minimal effort.

It turns out this is possible using a rather mysterious directive in nginx called post_action. It’s not even documented, so it’s plausible that it’s not recommended for long-term production use.

In the example below we accept POST’s to /api/foobar. The location directive matches exactly this URI so your needs may vary.

Each POST payload then gets proxied to localhost:8080 but due to debug_api_mirror, it also gets sent to localhost:9999.

Remember to test the nginx config with nginx -t and then restart nginx.

Let’s make something listen on port 9999. Posting something to the server in question on the target URL, we’ll see the connection also appearing here:

$ PORT=9999 http-echo-server
[server] event: listening (port: 9999)
[server] event: connection
[connection] event: data
--> POST .... etc

PS: Note that to install http-echo-server, you’ll need npm and install it via sudo npm install http-echo-server -g