Github Page Domain CDN Acceleration
05 Mar 2014 • Leave Comments- Default domain
The github default domain isusername.github.io
which is also a subdomain. But usually we need to use our own domain for blog. Personal domain may be top domain or subdomain. Subdomain is likewww.example.com
while top domain is likeexample.com
. Github namesA
,Alias
, andANAME
all asapex
domain (record). Hereapex
domain acctually is not a domain but a domain dns record. - Acceleration
The key is to set aCNAME
record for your github pageusername.github.io
. For example if you have a top domainexample.com
, ACNAME
record is like:
Name | Type | Target |
---|---|---|
www | CNAME | username.github.io |
This means subdomain address www.example.com
will be redirected to username.github.io
.
- CNAME and Top domain
CNAME
does not support top domain. The following three lines are all illegal unless your DNS provider supports CNAME flattening (i.e. Clouflare), ALIAS and ANAME.
Name | Type | Target |
---|---|---|
example.com | CNAME | username.github.io |
@ | CNAME | username.github.io |
CNAME | username.github.io |
If you set CNAME
like this, it will direct all your other subdomains like mail, ftp etc to your github page. Refer to custom subdomain.
Do not use wildcard DNS records (e.g. *.example.com) with GitHub Pages! A wildcard DNS record will allow anyone to host a GitHub Pages site at one of your subdomains.
Can I use my top domain for acceleration? Yes, but not easy! First, your DNS provider must support Alias DNS Record
which is not a standard DNS record. Only a few DNS provider support this with fees, like dnssimple. Refer to What is an ALIAS record? and Pointing the Domain Apex to Heroku.
- Procedures
-
Add a
CNAME
file (or by Github setting) at the root directory of github pages. You may find it uncessary at first. If you don't add this file, only the first time you enter the custom domain url works as you wish. If you further click some other links in the blog (i.e. a post), you will find the url changes to original username.github.io. This looks ugly. What was worse, The webbrower tab will not show the blog title.Add your custom domain to your GitHub Pages site's repository before configuring your domain name with your DNS provider.
- In the
CNAME
file, you can put only one line namely www.example.com there. Now Github handles everything in your jekyll blog with address www.example.com not the original username.github.io. Attention! Put bare address. Don't addhttp://
. - Next need DNS support. I choose freenom to register my top domain example.tk. Why top domain? Not subdomain? Don't worry! Freenom supports top domain, why not get one? Yes, get the top domain. Then we can set a CNAME DNS record! Refer to How to setup A, MX, CNAME….
- The
CNAME
record is to transferWWW
subdomain of example.tk (namely www.example.tk) to username.github.io. Now wait for a while, it will be fine. Usedig www.example.tk
to test your result. Refer to use the dig command. - But if you want example.tk also points to username.github.io, what should be done? As mentioned above we cannot use
CNAME
. What is worse, freenom does not supportAlias
DNS record likednssimple
does. However we can setA
record for the top domain alghtough it does not support CDN. Please refer to configuring an A record on Github.
Name | Type | Target |
---|---|---|
A | 192.30.252.153 | |
A | 192.30.252.154 |
- Now you can access your blog from both subdomain and top domain. Although top domain does not support CDN individually, but if both subdomain CNAME and top domain A records are configured, github itsefl will create redirect between the two based on the CNAME file in the github page root directory. Refer to Configuring a www subdomain. Since the
CNAME
file in github pages arewww.example.com
, then when you inputexample.com
in your browser, github will redirects you towww.example.com
.
For more detailed Top domain DNS CNAME discussion refer to 顶级域名不能设置DNS CNAME记录.
Reference