创建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.domainis the root domain linked to your Cloudflare zone (e.g.,example.com, notwww.example.comunless 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
1for this zone. If so, adjust your rule’s priority to an unused number (like2). - 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
相关产品推荐
相关产品推荐

