Sub topic 10
Configuring DNS records
This chapter assumes you have a site published with GitHub Pages and a domain name from a registrar. You should know what a DNS record is at a basic level. The previous chapter added the domain in the repository settings. Here you set up the records at your DNS provider so the domain reaches GitHub.
What DNS has to do
DNS (the Domain Name System) maps names to addresses. For GitHub Pages, you need DNS to answer two questions:
- Where does the bare domain, such as
example.com, point? This is the apex domain. - Where does a subdomain, such as
www.example.com, point?
Each question uses a different record type. An A record maps a name to an IPv4 address. A CNAME record maps a name to another name. The DNS standard does not allow a CNAME at the apex, because the apex also holds other records such as NS and SOA. That is why the apex uses A records and the subdomain uses a CNAME.
The records to create
The following records point an apex domain and a www subdomain at GitHub Pages. Replace example.com with your domain and USERNAME with your GitHub user or organization name.
A example.com 185.199.108.153
A example.com 185.199.109.153
A example.com 185.199.110.153
A example.com 185.199.111.153
CNAME www.example.com USERNAME.github.ioCreate all four A records. Each one is a separate record with the same name and a different address. GitHub lists four addresses so that the domain keeps working if one is unavailable. The CNAME target is your account's github.io hostname only. Do not include the repository name, even for a project site.
Many DNS providers use @ to mean the apex domain, and they may want www instead of the full www.example.com. Check how your provider writes names.
GitHub also documents AAAA records for IPv6. They are optional.
How a request reaches your site
The diagram shows what happens when a visitor opens your domain.
GitHub uses the Host header of the request to decide which site to serve. This is why the domain must also be set in the repository, either with the Custom domain setting or with a CNAME file in a branch-published site. The DNS records alone only get the request to GitHub.
Apex and www together
If you set a custom domain of example.com and also have the www CNAME record, GitHub redirects www.example.com to example.com. If you set www.example.com as the custom domain instead, the redirect goes the other way. Pick one as the canonical address and set that in the repository.
Checking your records
DNS changes can take from a few minutes to a day or more to spread, depending on the TTL (time to live) of the old records. You can check what the world sees with dig:
dig example.com +noall +answer -t A
dig www.example.com +noall +answer -t CNAMEThe first command should list the four GitHub addresses. The second should show USERNAME.github.io.. If the answers are correct and the Pages settings page still reports a DNS problem, wait and use the check again from the settings.
Common mistakes
- Using a CNAME at the apex. Most providers refuse it. Some offer ALIAS or ANAME records, which work but are provider-specific. The A records are the documented path.
- Leaving old A records in place. A leftover record pointing at a previous host sends some visitors to the wrong server. Remove it.
- Pointing the CNAME at the repository name, such as
USERNAME.github.io/repo. A DNS target is a hostname and cannot contain a path. - Using a wildcard record such as
*.example.com. GitHub warns against it, because it lets other people claim subdomains of your domain through their own Pages sites.
Exercise
Exercise
You own example.com. You created one A record for example.com pointing to 185.199.108.153 and a CNAME for www.example.com pointing to USERNAME.github.io/my-repo. Visitors report that the site is flaky and www does not resolve. Name two fixes.
Show answer
Add the other three A records (185.199.109.153, 185.199.110.153, 185.199.111.153) so all four are present. Change the CNAME target to USERNAME.github.io, with no path, because a DNS target must be a hostname only.
Exercise
Why can the apex domain not use a CNAME record?
Show answer
The DNS standard does not allow a CNAME to coexist with other records at the same name, and the apex always has NS and SOA records. So the apex uses A records and the subdomain uses a CNAME.