Alexandar Tzanov's Personal Blog

Azure Static Web Apps, Custom Domains, and Cloudflare

My profile website became inaccessible after switching to Cloudflare DNS from Google Domains DNS. I had to go this route because Google announced that they are going to sell the Google Domains service to Squarespace. For some reason this was not announced via email to Google Domains customers! At least I did not receive one. My guess is this was done on purpose – to avoid customers doing what I am in the process of doing and moving their domains to another service provider. You know, to avoid devaluing the Google Domains service if customers begin leaving before the deal is done.

After switching DNS, when trying to navigate to my profile site I was receiving the “ERR_TOO_MANY_REDIRECTS” error message. At first, I thought it was an issue related to the “WWW” check and redirect I have as one of the first steps in my template script. Something that has been in place for a very long time, but I assumed it might be interfering because of how the Cloudflare service works. I removed that particular step but the issue with too many redirects persisted. Next, I thought it might have something to do with the HTTPS redirect and re-write features provided by Cloudflare. Turning those off did not resolve the issue either.

At this point I began thinking about moving the domain to another registrar and utilizing the Azure DNS zone service. This was an option I was considering simply because Cloudflare is not just a DNS host service, but a whole plethora of other features and services it offers as man-in-the-middle proxy. One of the benefits of using Cloudflare is security between the client and the hosting service through its filters and caching. Here is where the solution to my issue laid.

After turning off DNS caching the too many redirects problem was resolved. I believe the cause was a combination of several factors:

  1. Microsoft recommends using a CNAME, even for the apex record, when setting up a custom domain for a Static Web App to take advantage of Azure’s global distribution system.
  2. Cloudflare does a CNAME flattening for the apex record, i.e. looks up the IP address of the target domain and uses said IP address for the apex record.
  3. Cloudflare caching requests content based on the target IP address.

My guess is that since my custom domains are configured as CNAMEs in my Static Web App, Azure is not expecting a content requested at a particular IP address and is not responding accordingly to what Cloudflare’s cache system is expecting. It could be an issue of Azure redirecting the requests through its global distribution system, which is being interpreted as a redirect loop. Though, it is more likely related to how the caching system is configured, since when it is turned off the is does not presents itself.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.