How to Create a Subdomain: Choosing Between A and CNAME DNS Records


2 views

When setting up a subdomain like blog.example.com or shop.example.com, you'll need to create a DNS record that tells the internet how to reach it. The two most common record types for this purpose are A and CNAME.

Use an A (Address) record when your subdomain points directly to an IP address:

blog.example.com.    IN  A    192.0.2.1

Key characteristics:

  • Direct mapping to IPv4 address
  • Best for when you control the server IP
  • Faster resolution (no extra lookup)
  • Common for root domains and static IP subdomains

Use a CNAME (Canonical Name) record when your subdomain should point to another domain name:

help.example.com.    IN  CNAME    help.zendesk.com.

Key advantages:

  • Points to another domain (useful for third-party services)
  • Automatically updates when target IP changes
  • Great for cloud services with changing IPs
  • Common for CDNs or SaaS integrations

Important rules to remember:

# Example of what NOT to do:
example.com.    IN  CNAME    othersite.com.  ; WRONG for root domain
  • Never use CNAME for root domain (@ or example.com)
  • A records can't point to domain names, only IPs
  • CNAME can't coexist with other records for same name
  • TTL (Time-To-Live) affects how quickly changes propagate

For a Node.js app running on a subdomain:

// If using direct server (A record)
dev.example.com.    IN  A    203.0.113.45

// If using Heroku (CNAME record)
staging.example.com.    IN  CNAME    quiet-rain-1234.herokudns.com.

After setting up your record:

  1. Use dig subdomain.example.com to verify
  2. Check propagation with nslookup
  3. Remember DNS changes can take up to 48 hours
  4. Clear local DNS cache if needed

When setting up a subdomain (e.g., blog.yourdomain.com), you need to configure DNS records to point it to the correct server. The two most common record types for this purpose are A and CNAME records.

A Record (Address Record):
Maps a domain/subdomain directly to an IP address.

example.com.    A     192.0.2.1
blog.example.com. A   192.0.2.1

CNAME Record (Canonical Name):
Aliases one name to another. Useful when pointing to another domain name rather than IP.

blog.example.com. CNAME example.com.

Use A record when:
- You know the exact IP address of your server
- The IP address won't change frequently
- You need to point directly to infrastructure

Use CNAME record when:
- You're pointing to another domain name (e.g., AWS, GitHub Pages)
- The IP might change often (cloud services)
- You want to delegate DNS management

Example 1: Basic A Record Setup

; Zone file excerpt for example.com
@        IN  A      192.0.2.1
www      IN  A      192.0.2.1
blog     IN  A      203.0.113.5

Example 2: CNAME for Cloud Service

; Pointing to GitHub Pages
blog     IN  CNAME  yourusername.github.io.

After setting up records:
- Use dig or nslookup to verify
- Consider TTL (Time-To-Live) values for changes
- Remember DNS propagation delays (typically 24-48 hours)

Dig command example:

dig blog.example.com +nostats +nocomments +nocmd

1. DNS Propagation: Changes aren't immediate
2. CNAME Chains: Avoid multiple CNAME redirects
3. Root Domain CNAME: Never use CNAME for root domain (@)