Can NGINX mirror the HTTP request on multiple destinations?

The short answer seems NO.

Why would one want to mirror the request incoming to one NGINX proxy on even one additional destination, let alone more? Well, maybe because you want to receive the production data on a test server temporarily, as it comes in – for some sort of analysis, or for creating some test data set?

Say, the normal setup is that you have an NGINX proxy sitting in front of a Java web server, and what you want to achieve is that in addition to sending the request to the main Java web server, the NGINX proxy should send 2 more copies to two extra Java web servers.

Forwarding to one additional destination is easy. On the main server (say, http://my.prod.server), configure the following in NGINX:

location ~ ^/my.url {
  proxy_pass http://$my_java_web_server;
  proxy_set_header Host $my_java_web_server;
  ...

  post_action @post_for_first_mirroring;
  ...
}

location @post_for_first_mirroring {
  proxy_ignore_client_abort on;
  ...
  proxy_pass http://my.test1.server:8080;
}

Whatever HTTP request you get on http://my.prod.server/my.url, will be forwarded to your additional destination http://my.test1.server:8080/my.url too.

Unfortunately, the same doesn’t work if you want to send two copies of the request to two different destinations, say.

If you try to configure more than one “post_action” directives in your location block, NGINX rejects it in its config-validation cycle saying “post_action” directive is duplicate.

post_action @post_for_first_mirroring;

post_action @post_for_second_mirroring;

If you try the following variant, it’s even more weird:

post_action @post_for_first_mirroring;

if ($some_flag_set_to_Yes = 'Yes') {
    post_action @post_for_second_mirroring;
}

Now it mirrors the request on the second destination, but not the first one and I found it quite interesting that instead of complaining clearly, it was silently executing only the second @post_action.

Just wanted to capture this bit of information here, in case anyone has similar needs.

Cheers.