如何设置cloudflare缓存内容但浏览器不缓存? (因此浏览器总是在每次刷新时从 CF 获取)

How to let cloudlfare to cache the content but make browser do not cache? (so browser always fetch from CF on every refresh)

以下代码告诉浏览器和 CloudFlare 不要缓存页面。 我需要 CloudFlare 来缓存内容。但是浏览器不缓存内容。

header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()+1) . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

我想要实现的用例是允许我清除缓存并让结果立即反映在访问者的浏览器上。 (我正在通过 API 动态清除缓存)

我从以下来源找到了问题的答案:https://support.cloudflare.com/hc/en-us/articles/202775670-How-Do-I-Tell-Cloudflare-What-to-Cache-

它提到:

if a customer would like to specify a cache timeout in the CDN which is different from the browser we can also use s-maxage:

Cache-Control: s-maxage=200, max-age=60

This will tell Cloudflare to cache the object for 200 seconds and the browser to cache the object for 60 seconds. Basically, s-maxage is intended to be followed ONLY by reverse proxies (so the browser should ignore it) whilst on the other hand, Cloudflare gives priority to s-maxage if present. Note that we will respect whichever value is higher: the browser cache setting in Cloudflare or the max-age header.

我在查找同一个问题时找到了这个答案。

有一种新的方法可以很方便地做到这一点,这似乎比 s-maxage 更好:您可以单独发送 CDN-Cache-Control and/or Cloudflare-CDN-Cache-Control headers.

使用这些新的 CDN-specific header 的优势在于它允许 Cloudflare 可以单独配置到可能沿途的任何其他代理缓存(独立于浏览器)。例如,如果您设置 Cloudflare-CDN-Cache-Control Cloudflare 尊重 然后从响应中删除 header。那么您的 cache-control header 将针对浏览器(设置为 no-cache)。

https://developers.cloudflare.com/cache/about/cdn-cache-control