You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Always Use HTTPS页面规则失败,请求排查Terraform 400错误

Troubleshooting Cloudflare Page Rule 400 Error When Enabling Always Use HTTPS

Let's break down the potential issues with your Terraform configuration and walk through fixes to resolve that 400 error.

First, here's your current rule for reference:

resource "cloudflare_page_rule" "https-only" { 
  zone = "${var.domain}" 
  target = "http://*${var.domain}/*" 
  priority = 1 
  actions = { always_use_https = true, } 
}

Most Likely Culprit: Incorrect Target Format

Cloudflare page rule targets don't need the http:// prefix when setting up an always_use_https rule. In fact, specifying the HTTP protocol here can conflict with how the rule is evaluated—since the rule’s purpose is to redirect HTTP traffic to HTTPS, the target should cover all requests to your domain regardless of protocol.

Fix the target line to this:

target = "*${var.domain}/*"

Other Potential Issues to Check

  • Zone Parameter Validation: Ensure var.domain is the root domain linked to your Cloudflare zone (e.g., example.com, not www.example.com unless that’s a separate zone). For more reliability, you can use your Cloudflare Zone ID instead of the domain name—you can find this in your domain’s overview page in the Cloudflare dashboard.
  • Priority Conflicts: Cloudflare doesn’t allow multiple page rules to share the same priority. Log into your Cloudflare dashboard and check if there’s already a rule using priority 1 for this zone. If so, adjust your rule’s priority to an unused number (like 2).
  • Outdated Terraform Provider: Older versions of the Cloudflare Terraform Provider might have compatibility issues with the latest Cloudflare API. Update your provider to the latest stable version by adding this to your Terraform config:
    terraform {
      required_providers {
        cloudflare = {
          source  = "cloudflare/cloudflare"
          version = "~> 4.0"
        }
      }
    }
    
  • Get Full Error Details: To pinpoint the exact issue, run terraform apply -debug—this will show you the complete API response from Cloudflare, including the specific error message that was truncated in your original output.

Revised Working Configuration

Here's the cleaned-up version of your rule with the target fix applied:

resource "cloudflare_page_rule" "https-only" { 
  zone     = var.domain 
  target   = "*${var.domain}/*" 
  priority = 1 
  actions  = { 
    always_use_https = true 
  } 
}

内容的提问来源于stack exchange,提问作者Anthony Kong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:00:13