Cloudflare 免费计划提供了三种 TLS 通用证书可选:Let’s Encrypt、Google Trust Services、SSL.com,均为三个月有效期。

默认情况下,Cloudflare 会自动使用 Let’s Encrypt 或 Google Trust Services 证书。如果想使用 SSL.com(或 DigiCert)证书,则需要通过 API 手动切换。

2025年3月更新:此前 DigiCert 一年期证书已停用,新增 SSL.com 三个月期证书。

一、获取必要信息

在开始之前,需要准备三样东西:Zone ID邮箱Global API Key

1. 获取 Zone ID

  1. 登录 Cloudflare Dashboard
  2. 进入目标域名
  3. 点击左侧 概述
  4. 页面右下角找到 API 区域
  5. 复制 区域 ID

2. 获取 Global API Key

  1. 点击右上角小人 → 我的个人资料
  2. 左侧菜单选择 API 令牌
  3. 找到 API 密钥
  4. 选择 Global API Key查看
  5. 邮箱验证,并复制 Key。

注意:API Key 相当于账号密码,请勿泄露给他人。

二、执行 API 请求

打开终端,执行以下命令:

1
2
3
4
5
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/你的Zone_ID/ssl/universal/settings" \
-H "X-Auth-Email: 你的Cloudflare邮箱" \
-H "X-Auth-Key: 你的Global_API_Key" \
-H "Content-Type: application/json" \
--data '{"certificate_authority":"证书类型"}'

证书类型参数

证书类型 参数值 兼容性 备注
Let’s Encrypt lets_encrypt 现代设备 默认自动使用
Google Trust Services google 主流浏览器 默认自动使用
SSL.com ssl_com 99.9% 浏览器/设备 备份证书,需通过 API 切换

例如,切换到 Let’s Encrypt:

1
2
3
4
5
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/你的Zone_ID/ssl/universal/settings" \
-H "X-Auth-Email: 你的Cloudflare邮箱" \
-H "X-Auth-Key: 你的Global_API_Key" \
-H "Content-Type: application/json" \
--data '{"certificate_authority":"lets_encrypt"}'

三、验证结果

执行成功后,返回的 JSON 中 success 应为 true

1
2
3
4
5
6
7
8
9
{
"result": {
"enabled": true,
"certificate_authority": "lets_encrypt"
},
"success": true,
"errors": [],
"messages": []
}

如果 successfalse,检查一下 API 参数是否填写正确。

命令行快速验证

1
curl -Iv https://你的域名.com 2>&1 | grep "issuer"

四、强制刷新证书

如果证书长时间未更新:

  1. 进入 Cloudflare 控制台
  2. 导航到 SSL/TLS边缘证书
  3. 禁用 通用 SSL,等几秒
  4. 重新启用 通用 SSL
  5. 清除浏览器缓存后访问网站查看

搞定 😋