跳转到内容
搜索文档

Terraform 示例

最后更新 查看 MarkdownAgent 设置

以下示例使用 Terraform 为 zone 定义单个 Cache Response Rule。该规则在缓存之前从 JavaScript 文件响应中剥离 Set-Cookie 和 ETag 标头。

Terraform cloudflare_ruleset resource

# Cache Response Rule to strip headers from JS responses
resource "cloudflare_ruleset" "cache_response_rules_example" {
  zone_id     = "<ZONE_ID>"
  name        = "Cache Response Rules"
  description = "Configure cache settings for origin responses"
  kind        = "zone"
  phase       = "http_response_cache_settings"

  rules {
    ref         = "strip_js_headers"
    description = "Strip caching headers from JS file responses"
    expression  = "http.request.uri.path.extension eq \"js\""
    action      = "set_cache_settings"
    action_parameters {
      strip_etags      = true
      strip_set_cookie = true
    }
  }

  rules {
    ref         = "tag_api_responses"
    description = "Tag API responses for targeted purging"
    expression  = "starts_with(http.request.uri.path, \"/api/\")"
    action      = "set_cache_tags"
    action_parameters {
      operation = "set"
      values    = ["api-response", "dynamic-content"]
    }
  }

  rules {
    ref         = "cache_control_200"
    description = "Set cache-control for successful responses"
    expression  = "http.response.code eq 200"
    action      = "set_cache_control"
    action_parameters {
      s_maxage {
        operation      = "set"
        value          = 86400
        cloudflare_only = true
      }
      must_revalidate {
        operation = "set"
      }
    }
  }
}

使用 Terraform 时,通过 ref 字段可在更新后保持稳定的规则 ID。添加该字段可防止 Terraform 在变更时重建规则。更多信息请参阅 故障排除(Terraform 文档)。

有关将 Terraform 与 Cloudflare 一起使用的更多指导,请参阅 Terraform

这篇文档对您有帮助吗?