Over the past few months, Hollow Developers has migrated servers into the Amazon EC2 environment. As part of this setup, a load balancer redirects traffic to a number of individual EC2 web server instances. A limitation to this, however, is that Amazon’s load balancers don’t work on root domains (for example, http://hollowdevelopers.com/, no www in front). The reason that these load balancers don’t work on root domains is because the DNS record must be a CNAME record, and not an A record. And, root domains at most DNS providers only allow A records.
CNAME and A Records: CNAME entries allow domains to create subdomains like ‘webmail.hollowdevelopers.com’, which can act as an alternative address to something like ‘google.com/a/hollowdevelopers.com’ – the CNAME record makes that long URL at another domain easy to remember. A records only allow IP addresses. Amazon Load Balancers require an entry like ‘hollowdevelopers-load-balancer.ec2.amazon.com’, so a CNAME entry is required.
So, this ultimately requires websites to use ‘www’ or something similar in front of their domain, since the ‘www’ record can be a CNAME record. As part of their sales pitch for their Route 53 DNS service, Amazon mentions that Route 53 allows you to place CNAME-type records into your root domain. However, we have always been happy with our DNS provider, CloudFlare. So, what is an easy way to ensure that all traffic goes through our load balancer?
On first glance, Hollow Developers was OK – our web servers automatically redirect users from the root domain to the www domain, primarily for consistency for search engine crawlers. However, in order for this to happen, the user would have already hit our server on the root domain. We wanted all traffic to go through the load balancer, regardless of the small number of hits that may come in through the root domain. This is where CloudFlare’s page rules came in.
CloudFlare page rules allow website owners to write redirect rules, allowing all traffic from the root domain to redirect to the www domain. Best of all, even free CloudFlare accounts allow a few page rules, meaning that anyone can use this trick for a free alternative to Amazon’s Route 53. Just a few rules will get you up and running:
- Forward http://hollowdevelopers.com/* to http://www.hollowdevelopers.com/$1
- Forward http://hollowdevelopers.com/ to http://www.hollowdevelopers.com/
The first rule will forward all pages on the domain to the exact same page on www. The second rule forwards the ‘naked’ root domain to the www domain. For more information on the syntax used, consult the CloudFlare documentation on the Page Rules interface.
There are numerous alternatives to this approach – including the use of Amazon’s Route 53 DNS service. However, we wanted to keep CloudFlare’s security and DDOS prevention features, so this was not an option we wanted to take. Have other alternatives? We would love to hear your comments/questions.