Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up DNS for an apex domain (no www) pointing to a Heroku app?

Tags:

heroku

dns

I already added a custom domain to my Heroku app and it works with www.domain.com.

I need to know how to set up the domain without www to resolve to the app, too.

Here are my current DNS settings:

$TTL 86400 @   IN SOA ns1.first-ns.de. postmaster.robot.first-ns.de. (     2013041500   ; serial     14400        ; refresh     1800         ; retry     604800       ; expire     86400 )      ; minimum  @                        IN NS      robotns3.second-ns.com. @                        IN NS      robotns2.second-ns.de. @                        IN NS      ns1.first-ns.de.  @                        IN A       88.198.38.XXX localhost                IN A       127.0.0.1 mail                     IN A       88.198.38.XXX ftp                      IN CNAME   www imap                     IN CNAME   www loopback                 IN CNAME   localhost pop                      IN CNAME   www relay                    IN CNAME   www smtp                     IN CNAME   www www                      IN CNAME   appname.herokuapp.com. @                        IN MX 10   mail 

What are the correct settings to use so that both example.com and www.example.com would point correctly to my Heroku app?

like image 488
mrks Avatar asked Apr 15 '13 18:04

mrks


People also ask

How can I use DNS without www?

Without the 'www', you must set your root (non-www) domain DNS A-record to point at your web server's IP address. This can be too rigid if you encounter availability or performance issues; the A-record is fixed and can take a day or two for changes to propagate.

How do I add a DNS to Heroku?

Summary of stepsAdd the custom domain to your app with the heroku domains:add command. Look up the Heroku-supplied DNS target for the custom domain using the heroku domains command. Configure your app's DNS provider to point to the Heroku-supplied DNS target. Confirm that your app is accessible via the custom domain.

How do I setup an apex domain?

Under Settings, select Custom domains. Select the + Add button. In the Enter domain tab, enter your apex domain name. For instance, if your domain name is example.com , enter example.com into this box (without any subdomains).


1 Answers

(Note: root, base, apex domains are all the same thing. Using interchangeably for google-foo.)

Traditionally, to point your apex domain you'd use an A record pointing to your server's IP. This solution doesn't scale and isn't viable for a cloud platform like Heroku, where multiple and frequently changing backends are responsible for responding to requests.

For subdomains (like www.example.com) you can use CNAME records pointing to your-app-name.herokuapp.com. From there on, Heroku manages the dynamic A records behind your-app-name.herokuapp.com so that they're always up-to-date. Unfortunately, the DNS specification does not allow CNAME records on the zone apex (the base domain). (For example, MX records would break as the CNAME would be followed to its target first.)

Back to root domains, the simple and generic solution is to not use them at all. As a fallback measure, some DNS providers offer to setup an HTTP redirect for you. In that case, set it up so that example.com is an HTTP redirect to www.example.com.

Some DNS providers have come forward with custom solutions that allow CNAME-like behavior on the zone apex. To my knowledge, we have DNSimple's ALIAS record and DNS Made Easy's ANAME record; both behave similarly.

Using those, you could setup your records as (using zonefile notation, even tho you'll probably do this on their web user interface):

@   IN ALIAS your-app-name.herokuapp.com. www IN CNAME your-app-name.herokuapp.com. 

Remember @ here is a shorthand for the root domain (example.com). Also mind you that the trailing dots are important, both in zonefiles, and some web user interfaces.

See also:

  • Doing DNS right with Heroku
  • Avoiding Naked Domains and DNS A-records

Remarks:

  • Amazon's Route 53 also has an ALIAS record type, but it's somewhat limited, in that it only works to point within AWS. At the moment I would not recommend using this for a Heroku setup.

  • Some people confuse DNS providers with domain name registrars, as there's a bit of overlap with companies offering both. Mind you that to switch your DNS over to one of the aforementioned providers, you only need to update your nameserver records with your current domain registrar. You do not need to transfer your domain registration.

like image 110
kch Avatar answered Oct 31 '22 19:10

kch