Developer技术博客 DevEngineer:林建有

Github自定义域名和域名解析设置

2018-07-21


域名注册

可以从网上搜索域名注册 关键词,进入域名注册提供商网站去注册自己想要的域名,看自己的情况选择购买合适价格的域名进行注册。

本人是在阿里云注册的域名,现在注册需要实名认证之后才能成功解析域名。否则在网站上输入自己的域名无法访问到ip地址,即打不开网站。

域名解析

本人注册的域名解析的是解析至GitHub主机上。

GitHub pages 是一个静态网页托管平台,你可以把你制作的HTML静态网页通过git工具上传至GitHub代码仓库。

在你GitHub上的某个项目上设置你的自定义域名

  1. 在GitHub上 , 导航至你的某个网页项目仓库 (GitHub Pages site’s repository)。
  2. Repository settings button在你的项目仓库下, 点击 **Settings **设置标签进入设置界面
  3. 在 “Custom domain,”下输入框中添加你的自定义域名(你之前购买的域名想要指向该项目作为网站,如:jaylin.top),之后点击 Save.按钮保存。 To check if your custom domain is supported, see “About supported custom domains.”
  4. 如果你设置了自定义域名之后可以强制为你的网站设置为 HTTPS 协议. For more information, see “Securing your GitHub Pages site with HTTPS.”

在DNS提供商网站上设置DNS解析记录

如图,在阿里云上,我的DNS解析记录包括Apex记录,subdomain记录:

阿里云DNS解析

Apex记录是指A记录是顶级域名解析设置,解析到的记录值是IP地址。eg:

记录类型 记录值
A 185.199.110.153 (这是github pages的IP地址)

subdomain记录是指CNAME子级域名记录解析设置,解析到github的网站域名。eg:

记录类型 记录值
CNAME username.github.io(你的gitbug pages的域名)
  1. A记录是为了能够通过你注册的Apex顶级域名eg:jaylin.top在浏览器上输入访问时能解析指定访问到GitHub pages主机上。

  2. subdomain子级域名就是在顶级域名前加上前缀的域名eg: shop.jaylin.top。想要让某个子级域名也指向GitHub pages主机上需要设置的类型是CNAME类型,在GitHub上的对应代码仓库下,按照上述设置自定义域名的方法设置主机怎么显示这个子级域名代表的网页内容。

GitHub支持的域名类型

Supported custom domain type Example domain
www subdomain www.example.com
one apex domain & one www subdomain example.com & www.example.com
apex domain example.com
custom subdomain blog.example.com

补充说明如何申请你的github pages域名

  1. github.com 上注册一个你的账号如:jaylin
  2. 创建一个代码仓库项目,名称是有要求的,前缀必须和你的注册的账号名称一样如:jaylin.gihub.io
  3. 然后这个代码仓库的项目名称:jaylin.gihub.io 就是你的GitHub pages的域名
  4. 通过git工具将你的网页代码上传这个项目仓库下,就可以通过http:// jaylin.gihub.io访问了。
  5. 如果你不满意这个仓库的域名,就可以自己注册一个自定义域名,步骤如上域名解析

自定义域名重定向至GitHub Pages site

The type of pages site you’re using determines how your site redirects custom domains.

Warning: If the URL for your GitHub Pages site contains a username or organization name that begins or ends in a dash, or contains consecutive dashes, then people browsing with Linux will receive a server error when they visit the site. To fix this, change your GitHub username to remove non-alphanumeric characters. For instructions on how to do this, see “Changing your GitHub username.”

Type of GitHub Pages site Pages default domain & host location on GitHub How the page is redirected Example custom domain
个人网站 username.github.io Automatically redirected to the custom domain that has been set for it user.example.com
企业 orgname.github.io Automatically redirected to the custom domain that has been set for it org.example.com
个人网站项目 username.github.io/projectname Automatically redirected to a subdirectory of a User Pages site custom domain (user.example.com/projectname) project.example.com
企业网站项目 orgname.github.io/projectname Automatically redirected to a subdirectory of an Organization Pages site custom domain (org.example.com/projectname) project.example.com

To learn more about the different GitHub Pages site types, including whether to use master or gh-pages as a publishing branch, see this User, Organization, and Project pages chart.


Comments

Content