Using nginx to send webhooks to multiple upstreams

Forwarding webhook requests from a single endpoint, to multiple ‘upstream’ endpoints could be useful in many scenarios. A lot of applications that dispatch webhooks only allow one url, however, which can be a bit of a limitation.

This pattern also simplifies maintenance. For instance, you can whitelist the relay IP while keeping your upstream endpoints on a separate network. Additionally, you could use a CNAME for the relay. This way, if you need to change the upstream endpoints, no modifications to the software sending the webhooks are necessary.

I created a simple repo containing some example config of how we could do this using nginx with the ngx_http_mirror_module.

The full nginx config is below

events {}

http {
    # Main server block
    server {
        listen 80;
        server_name localhost;

        location / {
            mirror /v1;
            mirror /v2;

            # this 'hack' needed so we can just return ok without a 'main' mirror
            proxy_pass http://localhost/accepted;
        }

        location /v1 {
            proxy_pass https://ensfbwkxlowrl.x.pipedream.net/;
        }
        location /v2 {
            proxy_pass https://en5qyv1bfk8u.x.pipedream.net/;
        }

         location /accepted {
            return 202;
        }
    }
}

It’s fairly self explanatory: the root `/` location mirrors requests to `/v1` and `/v2`

The /v1 and /v2 locations both proxy_pass to the upstreams

Finally, we have a `/accepted` location that simply returns 202
This is because the mirror module is designed to be used to simply mirror requests to *another* location – we need the default to return 202, while forwarding requests to v1 and v2.

Full repo:
https://github.com/alexjamesbrown/nginx-webhook-relay

Leave a Reply

Your email address will not be published. Required fields are marked *