跳转到内容
搜索文档

零停机迁移

最后更新 查看 MarkdownAgent 设置

当终端客户已在其他 CDN 上线时,将其 CNAME 切换到你的 Cloudflare fallback origin 会造成短暂窗口,在此期间 Cloudflare 尚无法代理其流量。预验证让你在 DNS 切换之前验证主机名所有权,并可选择预先签发 TLS 证书,从而使迁移无缝完成。

迁移顺序

  1. 通过 API 创建自定义主机名。
  2. 使用 HTTP token 或 DNS TXT 记录预验证主机名所有权。
  3. 在 DNS 切换前预先签发 TLS 证书。
  4. 确认主机名状态为 active
  5. 更新终端客户的 CNAME——流量无停机切换。

步骤 1:创建自定义主机名

调用 Create Custom Hostname 端点。记下响应中的 ownership_verificationownership_verification_http 字段——下一步将用到它们。

Create custom hostnamebash
curl https://api.cloudflare.com/client/v4/zones/{zone_id}/custom_hostnames \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --data '{
    "hostname": "app.example.com",
    "ssl": {
      "method": "http",
      "type": "dv",
      "settings": {
        "http2": "on",
        "min_tls_version": "1.2"
      }
    }
  }'
Example response (truncated)json
{
  "result": {
    "id": "24c8c68e-bec2-49b6-868e-f06373780630",
    "hostname": "app.example.com",
    "status": "pending",
    "verification_errors": ["custom hostname does not CNAME to this zone."],
    "ownership_verification": {
      "type": "txt",
      "name": "_cf-custom-hostname.app.example.com",
      "value": "0e2d5a7f-1548-4f27-8c05-b577cb14f4ec"
    },
    "ownership_verification_http": {
      "http_url": "http://app.example.com/.well-known/cf-custom-hostname-challenge/24c8c68e-bec2-49b6-868e-f06373780630",
      "http_body": "48b409f6-c886-406b-8cbc-0fbf59983555"
    },
    "created_at": "2020-03-04T20:06:04.117122Z"
  }
}

在此阶段,verification_errors 字段会显示 custom hostname does not CNAME to this zone——这是预期行为。预验证完成后该错误会清除。


步骤 2:预验证主机名所有权

选择适合终端客户情况的方法。

选项 A:HTTP token(终端客户不控制 DNS)

当终端客户无法更新其权威 DNS,或你希望自行处理验证时,使用此方法。

  1. 从 Create Custom Hostname 响应的 ownership_verification_http 对象中复制 http_urlhttp_body

  2. 让终端客户在其源站服务器上于 http_url 路径提供 http_body 值。例如,在 nginx 中:

    nginx examplenginx
    location /.well-known/cf-custom-hostname-challenge/24c8c68e-bec2-49b6-868e-f06373780630 {
        return 200 "48b409f6-c886-406b-8cbc-0fbf59983555\n";
    }

    Cloudflare 使用 User-Agent: Cloudflare Custom Hostname Verification 爬取此 URL。源站必须以 200 状态响应,且正文中为确切的 token 值。

  3. 等待几分钟以便 Cloudflare 爬取 token。所有权确认后,主机名状态将从 pending 变为 active

选项 B:TXT 记录(终端客户控制 DNS)

当终端客户可在其权威 DNS 提供商处添加 DNS 记录时,使用此方法。

  1. 从 Create Custom Hostname 响应的 ownership_verification 对象中复制 namevalue

  2. 让终端客户在其 DNS 提供商处添加 TXT 记录:

    Type Name Value
    TXT _cf-custom-hostname.app.example.com 0e2d5a7f-1548-4f27-8c05-b577cb14f4ec
  3. 等待几分钟以便 Cloudflare 检测到该记录。所有权确认后,主机名状态将变为 active

  4. 主机名变为 active 后,终端客户可以移除该 TXT 记录。


步骤 3:预先签发 TLS 证书

预先签发证书可确保切换期间不会出现 TLS 错误。若跳过此步骤,证书要到终端客户的 CNAME 指向 Cloudflare 之后才能签发,这意味着在 DNS 变更期间 ssl.status 将保持 pending。请选择以下方法之一:

  • Delegated DCV - 一次性 CNAME 记录将 _acme-challenge 委托给你的 SaaS zone,让 Cloudflare 自动处理所有未来续期。终端客户可在其自己的权威 DNS 处放置委托 CNAME;若你直接为客户托管 DNS,也可将其放在你自己的 zone 中。
  • TXT validation - 让终端客户在其权威 DNS 中添加 TXT 记录。通配符自定义主机名需要此方法。
  • Manual HTTP validation - 在源站的 /.well-known/ 路径提供 DCV token 文件。无需终端客户操作。

步骤 4:确认主机名为 active

在更新 DNS 之前,验证主机名与证书均已就绪。

Get custom hostname detailsbash
curl https://api.cloudflare.com/client/v4/zones/{zone_id}/custom_hostnames/{custom_hostname_id} \
  --header "Authorization: Bearer <API_TOKEN>"
Example response (truncated)json
{
  "result": {
    "id": "24c8c68e-bec2-49b6-868e-f06373780630",
    "hostname": "app.example.com",
    "status": "active",
    "ssl": {
      "status": "active"
    }
  }
}

在继续之前,请等待 result.statusresult.ssl.status 均为 active。若任一仍为 pending,请等待并再次轮询。


步骤 5:更新终端客户的 CNAME

一旦 result.statusactive(若你在步骤 3 预先签发了证书,则 ssl.status 也应为 active),让终端客户将其 CNAME 更新为指向你的 fallback origin:

Type Name Value
CNAME app fallback.yoursaaszone.com

DNS 传播后,流量将开始通过 Cloudflare 代理。由于主机名已验证且证书已签发,过渡期间不会出现停机或证书错误。


相关资源

这篇文档对您有帮助吗?