CNAMEs – how do they work?

CNAMEs are a neat trick of the domain name system, but are often unused or misunderstood.

Here’s a quick(ish) explanation of what CNAMEs are, how they work, and why you should use them.

CNAMEs are basically an alias of an existing domain. It’s easy to imagine that CNAMEs behave like internal redirects, but they’re much neater.

Aside: CNAME is shorthand for “canonical name”, and refers to the *relationship* that a subdomain – for example – has with the primary or “canonical” domain, rather than the entry itself. So for a CNAME record of 6 IN CNAME, the “canonical name” is actually the part right of “CNAME”.

If a domain is set up like so:

ross-air:~ ross$ dig +nostats +nocomments +nocmd
;  IN  A 6 IN  CNAME 513 IN  A

Then when a browser visits, the DNS resolver (usually part of your OS stack) actually sends a standard request for an A record for to the nameserver. If this lookup fails, the nameserver will check to see whether the record exists as a CNAME entry. When it finds it, the nameserver restarts the query using, which resolves to the IP

The browser sets the Host header of the request to (so that the receiving server knows what was originally requested) and continues with the request to the server.

When the server receives this message, it looks up the host it’s been passed and serves up the corresponding configured website.

It’s not just web browsers like Chrome, Firefox, etc that perform this task, tools like curl, wget, etc will perform the same lookup as part of the request.

CNAMEs are particularly useful when a lot of websites are being hosted at a single IP address. If all the domains hosted at that IP point to a single “gateway” domain, then only that gateway domain needs to point to an IP address, making Domain->IP address portability less of a headache.

If you’re using a web host or platform provider that deals with some of the hassle of server and domain administration for you, this is probably one of the tricks they use to make it possible to handle lots of users without breaking everything when an IP address somewhere needs to change.


This entry was posted in Infrastructure. Bookmark the permalink. Both comments and trackbacks are currently closed.

One Comment

  1. Wejn
    Posted September 13, 2012 at 11:25 am | Permalink

    Yes, CNAMEs are somewhat useful for “domain portability”. Except for one little detail…

    You can’t cname “” itself, thus rendering the usefulness of cnames to almost nil. Because if you want to point somewhere with a cname you’re stuck with 3rd level domains ( and the like).

    No way around that. :-/