Skip to main content

Managing a custom domain for your GitHub Pages site

You can set up or update certain DNS records and your repository settings to point the default domain for your GitHub Pages site to a custom domain.

GitHub Pages 适用于具有 GitHub Free 和组织的 GitHub Free 的公共仓库,以及具有 GitHub Pro、GitHub Team、GitHub Enterprise Cloud 和 GitHub Enterprise Server 的公共和私有仓库。 有关详细信息,请参阅“GitHub 的产品”。

People with admin permissions for a repository can configure a custom domain for a GitHub Pages site.

About custom domain configuration

Make sure you add your custom domain to your GitHub Pages site before configuring your custom domain with your DNS provider. Configuring your custom domain with your DNS provider without adding your custom domain to GitHub Enterprise Cloud could result in someone else being able to host a site on one of your subdomains.

The dig command, which can be used to verify correct configuration of DNS records, is not included in Windows. Before you can verify that your DNS records are configured correctly, you must install BIND.

Note: DNS changes can take up to 24 hours to propagate.

Configuring a subdomain

To set up a www or custom subdomain, such as www.example.com or blog.example.com, you must add your domain in the repository settings. After that, configure a CNAME record with your DNS provider.

  1. 在 GitHub Enterprise Cloud 上,导航到站点的仓库。

  2. 在存储库名称下,单击 “设置”。 “存储库设置”按钮

  3. In the "Code and automation" section of the sidebar, click Pages.

  4. Under "Custom domain", type your custom domain, then click Save. If you are publishing your site from a branch, this will create a commit that adds a CNAME file to the root of your source branch. If you are publishing your site with a custom GitHub Actions workflow , no CNAME file is created. For more information about your publishing source, see "Configuring a publishing source for your GitHub Pages site." Save custom domain button

    Note: If your custom domain is an internationalized domain name, you must enter the Punycode encoded version.

    For more information on Punycodes, see Internationalized domain name.

  5. Navigate to your DNS provider and create a CNAME record that points your subdomain to the default domain for your site. For example, if you want to use the subdomain www.example.com for your user site, create a CNAME record that points www.example.com to <user>.github.io. If you want to use the subdomain another.example.com for your organization site, create a CNAME record that points another.example.com to <organization>.github.io. The CNAME record should always point to <user>.github.io or <organization>.github.io, excluding the repository name. 有关如何创建正确记录的更多信息,请参阅 DNS 提供程序的文档。 有关站点默认域的更多信息,请参阅“关于 GitHub Pages”。

    警告:强烈建议勿使用通配符 DNS 记录,例如 *.example.com。 通配符 DNS 记录将允许任何人在你的其中一个子域上托管 GitHub Pages 站点,即使这些子域会进行验证也是如此。 有关详细信息,请参阅“验证 GitHub Pages 的自定义域”。

  6. 打开终端终端Git Bash

  7. To confirm that your DNS record configured correctly, use the dig command, replacing WWW.EXAMPLE.COM with your subdomain.

    $ dig WWW.EXAMPLE.COM +nostats +nocomments +nocmd
        > ;WWW.EXAMPLE.COM.                    IN      A
        > WWW.EXAMPLE.COM.             3592    IN      CNAME   YOUR-USERNAME.github.io.
        > YOUR-USERNAME.github.io.      43192   IN      CNAME   GITHUB-PAGES-SERVER .
        > GITHUB-PAGES-SERVER .         22      IN      A       192.0.2.1
  8. 如果你使用静态站点生成器在本地构建站点并将生成的文件推送到 GitHub Enterprise Cloud,请将添加 CNAME 文件的提交拉取到本地存储库。 有关详细信息,请参阅“排查自定义域和 GitHub Pages 的问题”。

  9. (可选)若要为站点强制实施 HTTPS 加密,请选择“强制实施 HTTPS”。 可能要过 24 小时才能使用此选项。 有关详细信息,请参阅“使用 HTTPS 保护 GitHub Pages 站点”。 对自定义域名强制使用 HTTPS 选项

Configuring an apex domain

To set up an apex domain, such as example.com, you must configure a custom domain in your repository settings and at least one ALIAS, ANAME, or A record with your DNS provider.

如果使用 apex 域作为自定义域名,建议还设置一个 www 子域。 如果通过 DNS 提供程序配置每种域类型的正确记录,GitHub Pages 将自动在域之间创建重定向。 例如,如果将 www.example.com 配置为站点的自定义域,并且为顶点和 www 域设置了 GitHub Pages DNS 记录,则 example.com 将重定向到 www.example.com。 请注意,自动重定向仅适用于 www 子域。 自动重定向不适用于任何其他子域,如 blog。 For more information, see "Configuring a subdomain."

  1. 在 GitHub Enterprise Cloud 上,导航到站点的仓库。

  2. 在存储库名称下,单击 “设置”。 “存储库设置”按钮

  3. In the "Code and automation" section of the sidebar, click Pages.

  4. Under "Custom domain", type your custom domain, then click Save. If you are publishing your site from a branch, this will create a commit that adds a CNAME file to the root of your source branch. If you are publishing your site with a custom GitHub Actions workflow , no CNAME file is created. For more information about your publishing source, see "Configuring a publishing source for your GitHub Pages site." Save custom domain button

  5. Navigate to your DNS provider and create either an ALIAS, ANAME, or A record. You can also create AAAA records for IPv6 support. 有关如何创建正确记录的更多信息,请参阅 DNS 提供程序的文档。

    • To create an ALIAS or ANAME record, point your apex domain to the default domain for your site. 有关站点默认域的更多信息,请参阅“关于 GitHub Pages”。
    • To create A records, point your apex domain to the IP addresses for GitHub Pages.
      185.199.108.153
      185.199.109.153
      185.199.110.153
      185.199.111.153
    • To create AAAA records, point your apex domain to the IP addresses for GitHub Pages.
      2606:50c0:8000::153
      2606:50c0:8001::153
      2606:50c0:8002::153
      2606:50c0:8003::153

    警告:强烈建议勿使用通配符 DNS 记录,例如 *.example.com。 通配符 DNS 记录将允许任何人在你的其中一个子域上托管 GitHub Pages 站点,即使这些子域会进行验证也是如此。 有关详细信息,请参阅“验证 GitHub Pages 的自定义域”。

  6. 打开终端终端Git Bash

  7. To confirm that your DNS record configured correctly, use the dig command, replacing EXAMPLE.COM with your apex domain. Confirm that the results match the IP addresses for GitHub Pages above.

    • For A records.
      $ dig EXAMPLE.COM +noall +answer -t A
      > EXAMPLE.COM    3600    IN A     185.199.108.153
      > EXAMPLE.COM    3600    IN A     185.199.109.153
      > EXAMPLE.COM    3600    IN A     185.199.110.153
      > EXAMPLE.COM    3600    IN A     185.199.111.153
    • For AAAA records.
      $ dig EXAMPLE.COM +noall +answer -t AAAA
      > EXAMPLE.COM     3600    IN AAAA     2606:50c0:8000::153
      > EXAMPLE.COM     3600    IN AAAA     2606:50c0:8001::153
      > EXAMPLE.COM     3600    IN AAAA     2606:50c0:8002::153
      > EXAMPLE.COM     3600    IN AAAA     2606:50c0:8003::153
  8. 如果你使用静态站点生成器在本地构建站点并将生成的文件推送到 GitHub Enterprise Cloud,请将添加 CNAME 文件的提交拉取到本地存储库。 有关详细信息,请参阅“排查自定义域和 GitHub Pages 的问题”。

  9. (可选)若要为站点强制实施 HTTPS 加密,请选择“强制实施 HTTPS”。 可能要过 24 小时才能使用此选项。 有关详细信息,请参阅“使用 HTTPS 保护 GitHub Pages 站点”。 对自定义域名强制使用 HTTPS 选项

Configuring an apex domain and the www subdomain variant

When using an apex domain, we recommend configuring your GitHub Pages site to host content at both the apex domain and that domain's www subdomain variant.

To set up a www subdomain alongside the apex domain, you must first configure an apex domain by creating an ALIAS, ANAME, or A record with your DNS provider. For more information, see "Configuring an apex domain."

After you configure the apex domain, you must configure a CNAME record with your DNS provider.

  1. Navigate to your DNS provider and create a CNAME record that points www.example.com to the default domain for your site: <user>.github.io or <organization>.github.io. Do not include the repository name. 有关如何创建正确记录的更多信息,请参阅 DNS 提供程序的文档。 有关站点默认域的更多信息,请参阅“关于 GitHub Pages”。
  2. To confirm that your DNS record configured correctly, use the dig command, replacing WWW.EXAMPLE.COM with your www subdomain variant.
    $ dig WWW.EXAMPLE.COM +nostats +nocomments +nocmd
        > ;WWW.EXAMPLE.COM                     IN      A
        > WWW.EXAMPLE.COM.              3592    IN      CNAME   YOUR-USERNAME.github.io.
        > YOUR-USERNAME.github.io.      43192   IN      CNAME   GITHUB-PAGES-SERVER.
        > GITHUB-PAGES-SERVER.         22      IN      A       192.0.2.1

Removing a custom domain

  1. 在 GitHub Enterprise Cloud 上,导航到站点的仓库。

  2. 在存储库名称下,单击 “设置”。 “存储库设置”按钮

  3. In the "Code and automation" section of the sidebar, click Pages.

  4. Under "Custom domain," click Remove. Save custom domain button

Securing your custom domain

如果 GitHub Pages 站点已禁用,但设置了自定义域,则存在域接管的风险。 在您的网站被禁用时拥有通过 DNS 提供商配置的自定义域,可能会导致其他人在您的一个子域上托管网站。

验证自定义域可防止其他 GitHub 用户将你的域用于他们的存储库。 如果你的域未经过验证,并且 GitHub Pages 站点已禁用,则应立即通过 DNS 提供商更新或删除 DNS 记录。 For more information, see "Verifying your custom domain for GitHub Pages."

Further reading