0

I am changing the domain name of my app from old.com to new.com

My app is connected to external APIs that send data through webhooks, and I would like to redirect the webhooks sent to the old url, so that they hit the new url and get processed.

I have a tried a plain rewrite

server {
  server_name old.com;
  listen 443 ssl;
  rewrite ^       https://new.com$request_uri;

I can see this triggers a 302 redirection, but that doesn't seem to do the trick..

I tried with a proxy_pass

server {
  server_name old.com;
  listen 443 ssl;

  location /webHook {
    proxy_pass https://new.com/webHook;
  }

  rewrite ^       https://new.com$request_uri;

Same result.. I can see a 302 redirect in the logs, but the webhook never seem to hit the new URL and doesn't get processed..

Any idea on how to achieve that?

Buno
  • 155
  • 2
  • 9

2 Answers2

0

The rewrite directives in the server block, which are not inside a location block are executed first (cf. ng_http_rewrite_module documentation).

So delete or comment the rewrite directive and the proxy_pass directive will get executed.

Piotr P. Karwasz
  • 5,748
  • 2
  • 11
  • 21
  • Thanks, indeed that did the trick - had to wrap the `rewrite` in a `location` block with a regexp that excluded my `/webHook` prefix and it works like a charm! – Buno Feb 09 '20 at 18:17
  • You just need to wrap it in `location /`, since prefix locations are selected based on the longest matching prefix. – Piotr P. Karwasz Feb 09 '20 at 18:19
0

Since the webhooks are not going to get a redirect (and thus, the only way for them to absorb your new app's domain is by your manual changes), why miss a trivial solution?

Simply add old.com under server where you have new.com:

server_name new.com old.com;
Danila Vershinin
  • 5,286
  • 5
  • 17
  • 21
  • I like the suggestion but would this not create SSL certificates issues, as the certificate registered in nginx conf file will be issued for new.com, and not for old.com? – Buno Feb 09 '20 at 11:49
  • That would be the downside, although if you have control of certificate issuance (e.g. `certbot`), you can generate a combined certificate that will be valid for both domains. – Danila Vershinin Feb 09 '20 at 11:56