使用 Rulesets API 通过 API 创建 cache rule。要配置 Cloudflare API,请参阅 API 文档。
通过 API 创建 cache rule 时,请确保:
- 将规则操作设置为
set_cache_settings。 - 根据您希望为匹配请求覆盖的设置在
action_parameters字段中定义参数。 - 将规则部署到
http_request_cache_settings阶段入口点 ruleset。
- 使用 List zone rulesets 方法获取
http_request_cache_settings阶段入口点 ruleset 中已存在的规则列表。 - 如果阶段 ruleset 不存在,使用 Create a zone ruleset 操作创建它。在新 ruleset 属性中,设置以下值:
- kind:
zone - phase:
http_request_cache_settings
- kind:
- 使用 Update a zone ruleset 操作将 cache rule 添加到 ruleset 规则列表。或者,在上一步提到的 Create a zone ruleset 请求中包含该规则。
- (可选)要更新现有 cache rule,使用 Update a zone ruleset rule 操作。有关示例,请参阅下面的部分。
这些示例将 zone 的所有 Cache Rules 设置为单个规则,因为直接使用这些示例将导致任何现有规则被删除。
Example: Cache everything for example.com
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(http.host eq \"example.com\")",
"description": "cache everything for example.com",
"action": "set_cache_settings",
"action_parameters": {
"cache": true
}
}
]
}'Example: Extend read timeout for Android clients
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(http.user_agent contains \"Android\")",
"description": "extend read timeout for android clients",
"action": "set_cache_settings",
"action_parameters": {
"cache": true,
"read_timeout": 300
}
}
]
}'Example: Disable Cache Reserve for frequently updated assets
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(starts_with(http.request.uri, \"/feed/\"))",
"description": "disable cache reserve for frequently updated assets",
"action": "set_cache_settings",
"action_parameters": {
"cache": true,
"cache_reserve": {
"enabled": false
}
}
}
]
}'Example: Turn off default cache TTLs
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(http.host eq \"example.com\")",
"description": "turn off default cache ttls",
"action": "set_cache_settings",
"action_parameters": {
"cache": true,
"edge_ttl": {
"mode": "bypass_by_default"
}
}
}
]
}'Example: Cache expected Vary responses
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(http.host eq \"example.com\")",
"description": "cache expected vary responses",
"action": "set_cache_settings",
"action_parameters": {
"cache": true,
"vary": {
"default": {
"action": "bypass"
},
"headers": {
"accept": {
"action": "normalize",
"media_types": [
"text/html",
"application/json"
]
},
"accept-language": {
"action": "normalize",
"languages": [
"en",
"fr",
"de"
]
}
}
}
}
}
]
}'Example: Update the position of an existing rule
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"expression": "(http.host eq \"example.com\")",
"description": "cache everything for example.com",
"action": "set_cache_settings",
"action_parameters": {
"cache": true
},
"enabled": true,
"position": {
"before": "da5e8e506c8e7877fe06cdf4c41add54"
}
}'用于 API 请求管理 Cache Rules 的 API 令牌必须具有以下权限:
- Zone > Cache Rules > Edit
- Account Rulesets > Edit
- Account Filter Lists > Edit