Wiring a CDN with CNAME and Managing Subdomains
Point a subdomain at your CDN with a CNAME, plan www/api/img/cdn subdomains, and confirm the change went live.
When you put a site behind a CDN, the usual move is to point a subdomain at the acceleration hostname your CDN provider hands you, using a CNAME record. This article covers how CNAMEs work, why the root domain can't use one, and how to lay out your subdomains.
What a CNAME does
A CNAME (alias record) points one name at another name rather than straight at an IP. A resolver follows the alias, then chases down the final A/AAAA record. That indirection is exactly what a CDN needs: the edge-node IPs behind the provider's hostname change often, but as long as you keep pointing at that hostname, the churn stays invisible to you.
A typical record reads:
cdn.example.com. CNAME d1a2b3c4.cdn-provider.net.
Why the root domain can't use a CNAME
CNAME comes with one hard rule: it cannot coexist with any other record on the same name. Your root domain (the apex, e.g. example.com) already carries mandatory SOA and NS records, so the DNS spec forbids putting a CNAME there.
Workarounds:
- ALIAS / ANAME — a CNAME-like record many DNS providers offer that is legal at the apex and answers as an A record.
- CNAME flattening — the DNS layer resolves the alias to an IP itself and returns that, giving the same result as ALIAS.
- If none of the above is available, 301-redirect the bare domain to the www subdomain and let that subdomain front the CDN.
Planning your subdomains
Split subdomains by role. It keeps responsibilities clear and lets you wire each one to a different backend:
- www — the main site
- api — your service endpoints
- img / static — images and static assets
- cdn — a single acceleration entry point
Send only static, cacheable subdomains through the CDN. Dynamic endpoints like api usually hit the origin directly or use a dedicated dynamic-acceleration path.
The general steps to wire a CDN
- In the CDN console, add a site and enter the hostname you want to accelerate (e.g. cdn.example.com).
- Copy the acceleration hostname the provider assigns you (something like xxxx.cdn-provider.net).
- At your DNS provider, create a CNAME pointing the subdomain at that acceleration hostname.
- Follow the prompts to set the origin address, cache rules, and an HTTPS certificate.
Confirming it took effect
After you save the record, wait for the old TTL to expire, then check with a command:
dig cdn.example.com CNAME +short
# or
nslookup -type=cname cdn.example.com
If the answer is the CDN's acceleration hostname, the delegation is in place. Then run curl -I https://cdn.example.com and look for a CDN cache header (such as X-Cache) in the response.
Summary
A CDN cutover boils down to one CNAME: the subdomain points at the provider's acceleration hostname, and the provider absorbs every IP change. The apex can't take a CNAME, so reach for ALIAS/flattening or a redirect to www. Carve your subdomains into clear www/api/img/cdn roles, then verify the record and the cache with dig and curl — and you've got a clean cutover.