When hosting multiple websites on a single server, the standard approach is to create A records for each domain pointing to the server's IP address. This creates maintenance headaches when you need to change servers:
example.com. IN A 192.0.2.1
blog.example.com. IN A 192.0.2.1
shop.example.com. IN A 192.0.2.1
Each time the server IP changes, you must update every individual A record across all DNS providers.
By implementing a CNAME strategy, you create a single point of control:
; Primary domain (you control this DNS)
main-server.example.net. IN A 192.0.2.1
; Client domains (can be at different providers)
example.com. IN CNAME main-server.example.net.
blog.example.com. IN CNAME main-server.example.net.
Now when changing servers, you only update the A record for main-server.example.net
.
Important implementation details:
- The target domain (
main-server.example.net
) must have a stable A record - MX records cannot point to CNAMEs (RFC 2181)
- Some providers may charge for CNAME records at the root domain (apex)
For root domains where CNAME isn't possible, consider:
; Using Cloudflare's ALIAS record
@ IN ALIAS main-server.example.net.
Other providers offer similar solutions (ANAME, ALIAS, etc.) that function like CNAMEs at the root.
Here's how to programmatically update your central A record using Python:
import requests
def update_dns_record(new_ip):
headers = {
"X-Auth-Email": "your@email.com",
"X-Auth-Key": "your_api_key",
"Content-Type": "application/json"
}
data = {
"type": "A",
"name": "main-server.example.net",
"content": new_ip,
"ttl": 300
}
response = requests.put(
"https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records/RECORD_ID",
headers=headers,
json=data
)
return response.json()
The additional DNS lookup adds minimal latency (typically 10-50ms). For most applications this is negligible compared to the maintenance benefits.
When hosting multiple websites on a single server, a common approach is to create individual A records for each domain pointing to your server's IP address. While this works, it creates maintenance headaches:
example.com. IN A 192.0.2.1
blog.example.com. IN A 192.0.2.1
shop.example.com. IN A 192.0.2.1
Every time you need to change servers or IP addresses, you must update each A record separately across multiple DNS providers.
Using CNAME records to point to a master domain you control solves this problem elegantly:
; Instead of A records
example.com. IN CNAME master.yourdomain.com.
blog.example.com. IN CNAME master.yourdomain.com.
; Then on your master domain
master.yourdomain.com. IN A 192.0.2.1
Now when your server IP changes, you only need to update the A record for master.yourdomain.com.
There are some technical details to consider when implementing this solution:
- Root Domain Limitations: RFC 1912 specifies that CNAME records shouldn't be used on zone apex (naked domain). For root domains, use ALIAS or ANAME records if your DNS provider supports them.
- TTL Management: Set appropriate TTL values (e.g., 300 seconds) for quick propagation when changing IPs.
- DNS Provider Support: Not all registrars support CNAME flattening for root domains.
Here's how you might implement this using Cloudflare's CNAME flattening feature:
; Zone file for example.com (hosted elsewhere)
@ IN CNAME master.yourdomain.com.
www IN CNAME master.yourdomain.com.
; Zone file for yourdomain.com (on Cloudflare)
master IN A 192.0.2.1
While CNAME resolution adds a small lookup overhead, modern DNS caching makes this negligible. The benefits of centralized management far outweigh the minimal performance impact.
For those who need more flexibility:
- DNS APIs: Automate updates using providers' DNS APIs (e.g., AWS Route 53, DigitalOcean DNS)
- Dynamic DNS: For frequently changing IPs, consider DDNS solutions