使用自定义域名访问GitHub Pages

在GitHub部署了静态网站后都是使用username.github.io进行访问,如果我们有自己的域名,可以通过自己的域名进行访问。

添加CNAME文件

在本地的source文件下新建一个名为:CNAME的文件,内容为你的域名。文件名必须是大写,域名只能有一个。

添加DNS解析

在DNS解析服务商处添加两条CNAME记录
DNS解析
检查DNS记录配置是否正确

  • IPv4
$ dig www.eyunbook.com +noall +answer -t A
www.eyunbook.com. 58 IN CNAME eastonsun.github.io.
eastonsun.github.io. 3598 IN A 185.199.108.153
eastonsun.github.io. 3598 IN A 185.199.109.153
eastonsun.github.io. 3598 IN A 185.199.111.153
eastonsun.github.io. 3598 IN A 185.199.110.153
  • IPv6
$ dig www.eyunbook.com +noall +answer -t AAAA
www.eyunbook.com. 30 IN CNAME eastonsun.github.io.
eastonsun.github.io. 30 IN AAAA 2606:50c0:8000::153
eastonsun.github.io. 30 IN AAAA 2606:50c0:8003::153
eastonsun.github.io. 30 IN AAAA 2606:50c0:8002::153
eastonsun.github.io. 30 IN AAAA 2606:50c0:8001::153

自定义域名

  • 打开静态网站的存储库
  • 点击Settings
  • 在左边栏找到Code and automation,点击下方的Pages
  • 在Custom domain中填入自己的域名,并勾先下方的Enforce HTTPS,此处我填写的是www的子域,这样访问顶级域名时会自动跳转到www子域

github设置