How to use Azure Application Gateway's rewrite rules feature to rewrite the hostname of a website?

2.1k Views Asked by At

I have a website called oldcompany.com. Our product name changed, and I would like to use Azure Application Gateway in front of the website, in order to rewrite the URL, i.e. access the website using the newcompany.com hostname.

There is a feature of Azure Application Gateway, called rewrite rules, that allows to modify request and response headers, documented here: https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/application-gateway/rewrite-http-headers-url.md#rewrite-http-headers-and-url-with-application-gateway. The reason I want to use this feature is that it allows specifying complex conditions to rewrite a header. So I don't want to be using the Override with new host name switch on the HTTP setting, but rather overwrite the Host header of all requests going through the application gateway.

I have defined:

  • the listener to listen on HTTPS, port 443, hostname newcompany.com
  • the backend pool pointing to oldcompany.com
  • the rule binding the listener to the backend pool

With this configuration only, accessing https://newcompany.com results in Azure Application Gateway's 502 error page, which is expected, because the Host header in the request is still newcompany.com, which is not a virtual host recognized by the server (which is only serving requests for oldcompany.com).

So, in order to set the Host header, I have configured a rewrite ruleset associated to my routing rule. This ruleset has a rule that changes the request's Host header to oldcompany.com.

However, I still get the same 502 error page when accessing https://newcompany.com. I have pulled the request from my application gateway's access logs, and the request shows the following fields:

host_s: oldcompany.com
originalHost_s: newcompany.com
httpStatus_d: 502

which seems like the correct values for the original and rewritten hosts.

What am I missing to make this work?

1

There are 1 best solutions below

0
Serhiy H V On

Regarding your configurations, you need to follow the next steps:

Add a backend.

Add a health probe.

Add backend settings and override with a specific domain name.

By default, the Application Gateway sends the same HTTP host header to the backend as it receives from the client. If your backend application/service requires a specific host value, you can override it using this setting.

Add a rule for the settings created above.