Cloudflare 可以在部署前验证规则或规则集。验证会检查完整配置,但不会持久化或发布更改。
验证包括:
- 表达式语法,以及字段、函数和运算符的可用性
- 操作、操作参数以及阶段兼容性
- 权限、套餐权益和规则配额
- 规则所用资源的引用
Cloudflare 仪表板会在部署前自动验证受支持的规则更改。如果验证失败,仪表板会显示错误,而不会发布该更改。
仪表板验证适用于 Security > Security rules 下的自定义规则和速率限制规则。
前往 Security rules(安全规则) ↗在 Rules > Overview 下创建或更新规则时也可使用。
前往 Overview(概览) ↗受支持的 Rulesets API 变更端点接受 dry_run=true 查询参数。你可以在账户或 zone(站点区域)级别的 POST、PUT、PATCH 和 DELETE 操作中使用此参数。
试运行会执行与所请求操作相同的授权和服务器端验证检查。它不会创建、更新、删除或发布任何配置。
dry_run 仅接受 true 和 false。省略该值时默认为 false。任何其他值都会返回 400 响应。
以下请求会验证规则更新,但不会应用该更新:
Required API token permissions
At least one of the following token permissions is required:Response Compression WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID?dry_run=true" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"action": "block",
"expression": "ip.src.country eq \"GB\"",
"description": "Block requests from the United Kingdom",
"enabled": true
}'API 返回的错误和状态码与对应的写入操作相同。通常返回 200 的成功操作会返回 result: null。通常返回 204 No Content 的操作仍会返回 204。
有关受支持的操作和请求 schema,请参阅 Rulesets API 参考。