What is CNAME Flattening?

( It would mean the world if you would consider following @colhountech on twitter. It won’t cost you anything but it gives me great encouragement to continue writing these posts)

CNAME Flattening is a way for cloudflare.com to allow you to add a CNAME entry for the root level of a domain.

For example, using the domain colhountech.com as a root level domain, the following CNAME is valid:

CNAME    www   colhountech.com
A        @     192.168.2.1
Another record shares the same name, so we've applied CNAME flattening.

I can set a CNAME alias to point www.colhountech.com to the root level domain colhountech.com and I can then assign an IP address to the root level domain of colhountech.com

Problem is that when you create a webapp or website on a cloud provider such as AWS or Azure, you most likely will prefer to use a CNAME so that if the IP address changes with your cloud provider, you don’t have to update your DNS settings.

However if I set a CNAME at the root level domain, the DNS Spec doesn’t allow any more entries for that root level domain. For example, if I set the following…

CNAME @ mywebsite.azurewebsites.net ; This is invalid and breaks the DNS Spec

If I do this on a ‘normal’ dns server, then I am not allowed to set a SOA or MX record for colhountech.com, it breaks my email and everything in my domain.

Solution: Cloudflare allows CNAME at the root level domain on their name servers only because rather than returning the CNAME record directly, they lookup the CNAME chain until they find a valid A Record (An IP Address), and return that IP address as a A record.

Effectively, they are flattening the chain.

Cool, eh?

References: