如何在 Github Pages 上设置自定义域名

2025-06-07

如何在 Github Pages 上设置自定义域名

假设您已经购买了自定义域名(example.net)并设置了Github 页面(example.github.io):

创建 CNAME 文件

CNAME在项目的根文件夹中创建一个包含自定义域的文件名example.com

# Outputs example.net to a file named CNAME
echo example.net > CNAME
Enter fullscreen mode Exit fullscreen mode

替代文本

您可能需要配置构建命令以将其复制CNAME到构建文件夹,例如使用package.json带有 React 的文件:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && cp CNAME build/CNAME",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
Enter fullscreen mode Exit fullscreen mode

配置您的域

在您的域名提供商(您购买域名的地方 - 我推荐porkbun.com)处,找到自定义主机/资源记录的配置并设置:

Type: A
Host: @
Answer/Data/Value: 185.199.108.153
TTL: 30min (or any)
Enter fullscreen mode Exit fullscreen mode

对所有 4 个 GitHub 页面 IP 地址执行此操作185.199.108.153185.199.109.153185.199.110.153185.199.111.153

您还需要添加以下内容:

Type: CNAME
Host: www
Answer/Data/Value: example.github.io
TTL: 30min (or any)
Enter fullscreen mode Exit fullscreen mode

替代文本

配置存储库

在 Github 上,前往代码库设置,并在 Github Pages 部分添加你的自定义域名。
这些更改和/或 HTTPS 更改可能需要最多 24 小时才能生效。

替代文本

参考:

文章来源:https://dev.to/heymarkkop/how-to-set-up-a-custom-domain-on-github-pages-hc7
PREV
我的最佳游标技巧 (v0.43) 📚 基础知识 🕵️ 代理 💬 预提示
NEXT
IT 人才配置行业的肮脏秘密