2

I have a Sitecore 9.3 xp1 scaled application hosted in Azure PaaS. The CD App Service sits behind an F5 and Azure Gateway, with DNS of https://mysite.com routing through these 2 proxies (F5 -> Gateway -> App Service). The App Service itself is hosted at https://mysite.azurewebsites.net.

I have configured my site's targetHostName attribute to be mysite.com accordingly. I have also instructed the Media Manager and Link Manager to include server URL when generating links.

Problem: Whenever a Link or Image URL is generated by Sitecore, the URL always includes https://mysite.azurewebsites.net as the hostname.

I have debugged the HttpContext.Current.Request and found these values being set:

{
    "Headers": {
        "Host": "mysite.azurewebsites.net",
        "Max-Forwards": "9",
        "X-Forwarded-For": "<ip address list>",
        "X-FORWARDED-PROTO": "https",
        "X-FORWARDED-PORT": "443",
        "X-ORIGINAL-HOST": "mysite.com",
        "X-Original-URL": "/some/local/path",
        "DISGUISED-HOST": "mysite.azurewebsites.net",
        "WAS-DEFAULT-HOSTNAME": "mysite.azurewebsites.net",
        "X-AppService-Proto": "https"
    },
    "HttpMethod": "GET",
    "IsSecureConnection": true,
    "Url": "https://mysite.azurewebsites.net/some/local/path",
    "UrlReferrer": null,
    "RawUrl": "/some/local/path"
}

I believe Sitecore is generating URLs based on the HttpContext.Current.Url or the Host header. Without Hijacking and modifying the Link/Media managers, how do I get the URLs to come out according to the X-ORIGINAL-HOST header?

1
  • Current site context is resolved correctly and the site's targetHostName is set to mysite.com accordingly. Jun 19 '20 at 18:34
3

If you're not using AlwaysIncludeServerUrl in your LinkManager options, it won't try to generate an absolute URL using the targetHostName property in your site definition - therefore, in this case the targetHostName property won't have any effect as all the URLs in the HTML will be relative.

It sounds like what's happening is that the Azure Application Gateway is accessing the backend pool/app service with the mysite.azurewebsites.net URL and you'll notice that in the HTTP request data you have above, the request host header is mysite.azurewebsites.net which means your response host header will also be mysite.azurewebsites.net.

To resolve this, you'll want to set up a rule to rewrite the response HTTP header in the Application Gateway to return the correct response header back through to the user. Microsoft conveniently has an example under the Modify a redirection URL heading in this link: https://docs.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers#common-scenarios

EDIT: The above answer doesn't work so well for a multisite instance - if you have a multisite Sitecore instance that needs to respond to separate hostnames, by default the application gateway will forward the request to *.azurewebsites.net URL. However, Sitecore will process the request with that same *.azurewebsites.net URL instead of the requested hostname. Azure Application Gateway forwards the requested URL through the X-ORIGINAL-HOST header. You'll need to use rewrite rules to rewrite the HOST header with the value of the X-ORIGINAL-HOST header as Sitecore will resolve the site using the host header and return the correct site context.

More details here: https://georgechang.io/posts/2020/sitecore-multisite-support-with-azure-application-gateway-and-app-services/

0

Here's the solution which ultimately worked:

  1. Prove domain ownership by creating an awverify TXT dns entry in DNS: https://docs.microsoft.com/en-us/azure/app-service/manage-custom-dns-migrate-domain#bind-the-domain-name-preemptively
  2. Add domain name to the app service (dns is still pointing to F5). Set an SSL binding and enable SSL only mode. Upload the certificate for your domain accordingly.
  3. On the app gateway, go to http settings > select your appropriate setting > host name section, and specify a domain override and set it to the canonical dns name.

After doing this, the "Host" header coming into the App Service is set to the proper application hostname, and Sitecore resolves link creation correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.