跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

可用设置

最后更新 查看 MarkdownAgent 设置

这些是创建 Cache Response Rule 时可以配置的设置。由于 Cache Response Rules 在 Cloudflare 收到源站响应后执行,请求和响应字段都可用于规则匹配。

表达式字段

请求字段

字段 类型 说明
http.cookie 字符串 完整的 Cookie 标头值
http.host 字符串 源站收到的 Host 标头;如果 Origin Rule 对其进行了更改,该值可能与请求的主机名不同
http.referer 字符串 HTTP Referer 标头
http.user_agent 字符串 HTTP User-Agent 标头
http.request.method 字符串 HTTP 请求方法
http.request.uri 字符串 请求 URI
http.request.uri.path 字符串 URI 路径
http.request.uri.path.basename 字符串 URI 路径的基名
http.request.uri.path.extension 字符串 URI 路径中的文件扩展名
http.request.uri.query 字符串 查询字符串
http.request.uri.args 映射 以键值对形式表示的查询字符串参数
http.request.uri.args.names 数组 查询字符串参数名称
http.request.uri.args.values 数组 查询字符串参数值
http.request.full_uri 字符串 包含协议和主机的完整请求 URI
http.request.headers 映射 以键值对形式表示的请求标头
http.request.headers.names 数组 请求标头名称
http.request.headers.values 数组 请求标头值
http.request.cookies 映射 以键值对形式解析的 Cookie
http.request.accepted_languages 数组 解析后的 Accept-Language 标头值

响应字段

字段 类型 说明
http.response.code 整数 来自源站的 HTTP 响应状态码
http.response.headers 映射 以键值对形式表示的响应标头
http.response.headers.names 数组 响应标头名称
http.response.headers.values 数组 响应标头值

如果您选择 Edit expression 选项,可以输入上述任何响应字段。

函数

此阶段提供以下函数:

  • all
  • any
  • concat
  • decode_base64
  • ends_with
  • len
  • lookup_json_integer
  • lookup_json_string
  • lower
  • regex_replace
  • remove_bytes
  • remove_query_args
  • split
  • starts_with
  • substring
  • to_string
  • upper
  • url_decode
  • wildcard_replace

有关每个函数的说明,请参阅函数。

运算符

完整运算符列表,请参阅运算符。

可用操作

Cache Response Rules 支持三种操作:

操作 说明
set_cache_settings 在缓存前从源站响应中剥离标头(ETag、Set-Cookie、Last-Modified)。
set_cache_tags 在响应上添加、移除或设置缓存标签,用于精准清除。
set_cache_control 修改源站响应中的 Cache-Control 标头指令。

操作:set_cache_settings

配置与源站响应缓存相关的设置。可用参数如下:

参数 类型 说明
strip_etags 布尔值 在缓存前从源站响应中剥离 ETag 标头。
strip_set_cookie 布尔值 在缓存前从源站响应中剥离 Set-Cookie 标头。
strip_last_modified 布尔值 在缓存前从源站响应中剥离 Last-Modified 标头。

API 信息

API 操作:set_cache_settings。

API configuration examplejson
"action_parameters": {
  "strip_etags": true,
  "strip_set_cookie": true,
  "strip_last_modified": true
}

完整 API 示例请参阅通过 API 创建规则。


操作:set_cache_tags

修改与响应关联的缓存标签。缓存标签可用于精准缓存清除。

参数 类型 说明
operation 字符串 必填。 取值之一:add、remove、set。
values 数组 缓存标签字符串列表,与 expression 互斥。
expression 字符串 计算结果为缓存标签数组的表达式,与 values 互斥。

API 信息

API 操作:set_cache_tags。

API configuration example (static values)json
"action_parameters": {
  "operation": "set",
  "values": ["api-response", "dynamic-content"]
}
API configuration example (expression)json
"action_parameters": {
  "operation": "add",
  "expression": "split(http.response.headers[\"Surrogate-Keys\"][0], \",\", 1)"
}

完整 API 示例请参阅通过 API 创建规则。


操作:set_cache_control

修改源站响应中的 Cache-Control 标头指令。

支持的指令

带时长值(秒)的指令:

  • max-age
  • s-maxage
  • stale-if-error
  • stale-while-revalidate

带可选限定符(标头名称)的指令:

  • private
  • no-cache

布尔指令:

  • no-store
  • no-transform
  • must-revalidate
  • proxy-revalidate
  • must-understand
  • public
  • immutable

指令配置

可用参数取决于指令类型。

带时长值的指令

适用于 max-age、s-maxage、stale-if-error 和 stale-while-revalidate。

参数 类型 说明
operation 字符串 必填。 set 或 remove。
cloudflare_only 布尔值 启用后,此设置仅影响 Cloudflare 缓存内容的方式,访问者仍会收到原始指令。
value 整数 以秒为单位的时长。当 operation 为 set 时必填。
带可选限定符的指令

适用于 private 和 no-cache。

参数 类型 说明
operation 字符串 必填。 set 或 remove。
cloudflare_only 布尔值 启用后,此设置仅影响 Cloudflare 缓存内容的方式,访问者仍会收到原始指令。
qualifiers 数组 用于限定指令的可选标头名称列表。
布尔指令

适用于 no-store、no-transform、must-revalidate、proxy-revalidate、must-understand、public 和 immutable。

参数 类型 说明
operation 字符串 必填。 set 或 remove。
cloudflare_only 布尔值 启用后,此设置仅影响 Cloudflare 缓存内容的方式,访问者仍会收到原始指令。

API 信息

API 操作:set_cache_control。

API configuration examplejson
"action_parameters": {
  "max-age": {
    "operation": "set",
    "value": 3600,
    "cloudflare_only": true
  },
  "stale-if-error": {
    "operation": "remove"
  }
}

完整 API 示例请参阅通过 API 创建规则。

这篇文档对您有帮助吗?