跳转到内容
搜索文档

3 – 配置 HTTPS 设置

最后更新 查看 MarkdownAgent 设置

设置基本 DNS 记录后,您可以使用 Terraform 配置区域设置。本教程演示了如何使用更新后的 v5 提供程序启用 TLS 1.3自动 HTTPS 重写 (Automatic HTTPS Rewrites)严格的 SSL 模式 (Strict SSL mode)

先决条件

  • 已完成教程 12
  • 源服务器上的有效 SSL 证书(使用 Cloudflare Origin CA 生成一个用于严格 SSL 模式的证书)

1. 创建区域设置配置

创建一个新分支并添加区域设置:

git checkout -b step3-zone-settings

将以下内容添加到您的 main.tf 文件中:

# 启用 TLS 1.3
resource "cloudflare_zone_setting" "tls_1_3" {
  zone_id    = var.zone_id
  setting_id = "tls_1_3"
  value      = "on"
}

# 启用自动 HTTPS 重写
resource "cloudflare_zone_setting" "automatic_https_rewrites" {
  zone_id    = var.zone_id
  setting_id = "automatic_https_rewrites"
  value      = "on"
}

# 将 SSL 模式设置为 strict
resource "cloudflare_zone_setting" "ssl" {
  zone_id    = var.zone_id
  setting_id = "ssl"
  value      = "strict"
}

2. 预览并应用更改

查看建议的更改:

terraform plan

预期输出

Plan: 3 to add, 0 to change, 0 to destroy.

Terraform will perform the following actions:

  # cloudflare_zone_setting.automatic_https_rewrites will be created
  + resource "cloudflare_zone_setting" "automatic_https_rewrites" {
      + setting_id = "automatic_https_rewrites"
      + value      = "on"
      + zone_id    = "your-zone-id"
    }

  # cloudflare_zone_setting.ssl will be created
  + resource "cloudflare_zone_setting" "ssl" {
      + setting_id = "ssl"
      + value      = "strict"
      + zone_id    = "your-zone-id"
    }

  # cloudflare_zone_setting.tls_1_3 will be created
  + resource "cloudflare_zone_setting" "tls_1_3" {
      + setting_id = "tls_1_3"
      + value      = "on"
      + zone_id    = "your-zone-id"
    }

提交并合并更改:

git add main.tf
git commit -m "Step 3 - Enable TLS 1.3, automatic HTTPS rewrites, and strict SSL"
git checkout main
git merge step3-zone-settings
git push

在应用更改之前,尝试使用 TLS 1.3 进行连接。从技术上讲,在默认设置下,您应该无法连接。为了顺次进行此测试,您需要针对 BoringSSL 编译 curl

curl -v --tlsv1.3 https://www.example.com 2>&1 | grep "SSL connection\|error"

如上所示,您应该会收到一条错误消息,因为您的区域尚未启用 TLS 1.3。通过运行 terraform apply 启用它并重试。

应用配置:

terraform apply

在系统提示时输入 yes

3. 验证设置

尝试与之前相同的命令。该命令现在将成功执行。

curl -v --tlsv1.3 https://www.example.com 2>&1 | grep "SSL connection\|error"

这篇文档对您有帮助吗?