CDN-Cache-Control 是在源站设置的响应标头字段,用于将 CDN 缓存的行为与其他可能处理响应的中间服务分开控制。您可以使用与 Cache-Control 相同的指令设置 CDN-Cache-Control 或 Cloudflare-CDN-Cache-Control 响应标头。
您有几个选项可确定 CDN-Cache-Control 指令如何与 Cache-Control 指令交互。
如果 Cache Response Rule 使用 set_cache_control 操作设置 Cache-Control 指令,这些指令优先于源站设置的任何 Cloudflare-CDN-Cache-Control 和 CDN-Cache-Control 标头。
当没有 Cache Response Rule 适用时,源站可以:
-
返回
CDN-Cache-Control响应标头,Cloudflare 据此评估以做出缓存决定。如果源站也返回Cache-Control,则按原样代理,不影响 Cloudflare 做出的缓存决定。此外,CDN-Cache-Control会向下游代理,以防 Cloudflare 和浏览器之间还有其他 CDN。 -
返回
Cloudflare-CDN-Cache-Control响应标头。这与源站返回CDN-Cache-Control的行为相同,但 Cloudflare 不会向下游代理Cloudflare-CDN-Cache-Control,因为它是仅用于控制 Cloudflare 的标头。如果您希望只有 Cloudflare 具有不同的缓存行为,而所有其他下游服务器依赖Cache-Control,或者您不希望 Cloudflare 向下游代理CDN-Cache-Control标头,此选项很有用。 -
同时返回
Cloudflare-CDN-Cache-Control和CDN-Cache-Control响应标头。在这种情况下,Cloudflare 在做出缓存决定时仅查看Cloudflare-CDN-Cache-Control,因为它是CDN-Cache-Control的最具体版本,并向下游代理CDN-Cache-Control。在此情况下仅转发CDN-Cache-Control很有用,如果您希望 Cloudflare 与下游其他 CDN 具有不同的缓存行为。
此外,surrogate 不会遵循来自源站响应中的 Cache-Control 标头。例如,如果响应中存在 Surrogate-Control 标头,Cloudflare 会忽略任何 Cache-Control 指令,即使 Surrogate-Control 标头不包含指令。
Edge Cache TTL cache rule 覆盖资源在边缘(Cloudflare 数据中心)缓存的时间。此 cache rule 覆盖 Cloudflare-CDN-Cache-Control/CDN-Cache-Control 中管理资源在边缘缓存时长的指令。您可以在仪表板的 Caching(缓存) > Cache Rules(缓存规则) 中创建此规则。
Browser Cache TTL cache rule 覆盖浏览器/Cloudflare 下游服务器缓存资源的时间。Browser Cache TTL 仅修改 Cache-Control 响应标头。此 cache rule 不会修改 Cloudflare-CDN-Cache-Control/CDN-Cache-Control 响应标头。
源站返回 Expires 响应标头,指定对象对浏览器而言被视为 stale 之前的时间。使用 Cloudflare-CDN-Cache-Control/CDN-Cache-Control 时,此响应标头不影响 Cloudflare 的缓存决定。
在 Cloudflare 未收到 Cloudflare-CDN-Cache-Control、CDN-Cache-Control 或 Cache-Control 值的情况下,可缓存资源使用通用默认值。
当您希望分别为源站缓存、CDN 缓存和浏览器缓存管理缓存资源的 TTL 时使用 CDN-Cache-Control。以下示例展示如何使用源站设置的响应标头管理缓存资源的 TTL。
标头:
Cache-Control: max-age=14400, s-maxage=84000Cloudflare-CDN-Cache-Control: max-age=24400CDN-Cache-Control: max-age=18000
缓存行为:
Caches | Cache TTL (seconds) | ||||||||
|---|---|---|---|---|---|---|---|---|---|
Origin Server Cache | 14400 | ||||||||
Network Shared Cache | 84000 | ||||||||
Cloudflare Edge | 24400 | ||||||||
Other CDNs | 18000 | ||||||||
Browser Cache | 14400 |
将 CDN-Cache-Control 标头与 Cache-Control 标头结合使用,以指定在错误或重新验证期间何时提供 stale 内容。以下示例展示如何设置标头和指令以在错误处理时应用于 CDN。
标头:
Cache-Control: stale-if-error=400Cloudflare-CDN-Cache-Control: stale-if-error=60CDN-Cache-Control: stale-if-error=200
响应 5XX 错误时的行为:
Caches | Stale served (seconds) in response to error | ||||||||
|---|---|---|---|---|---|---|---|---|---|
Origin Cache Layer/Network Cache/Browser Cache | 400 (if it assumes the directive applies) | ||||||||
Cloudflare Edge | 60 | ||||||||
Other CDN | 200 |