问题描述
我正在尝试缓存一些html页面,就像通过CloudFlare自动缓存图像的方式一样,但是我无法让CloudFlare实际访问其html缓存.
I am trying to get some html pages to be cached, the same way images are automatically cached via CloudFlare but I can't get CloudFlare to actually hits its cache for html.
根据文档(请参阅: https://support.cloudflare.com/hc/zh-CN/articles/202775670-How-Do-I-Tell-CloudFlare-What-to-Cache- ),可以将Cache-Control设置为public
且max-age
大于0的任何内容进行缓存.
According to the documentation (Ref: https://support.cloudflare.com/hc/en-us/articles/202775670-How-Do-I-Tell-CloudFlare-What-to-Cache-), it's possible to cache anything with a Cache-Control set to public
with a max-age
greater than 0.
我在原始Nginx服务器上尝试了各种标头组合,但均未成功.从简单的Cache-Control: public, max-age=31536000
到更复杂的标头,包括s-maxage=31536000
,Pragma: public
,ETag: "569ff137-6"
,Expires: Thu, 31 Dec 2037 23:55:55 GMT
,没有任何结果.
I've tried various combinations of headers on my origin Nginx server without success. From a simple Cache-Control: public, max-age=31536000
to more complex headers including s-maxage=31536000
, Pragma: public
, ETag: "569ff137-6"
, Expires: Thu, 31 Dec 2037 23:55:55 GMT
without any results.
是否有任何强制CloudFlare从其缓存中提供HTML页面的想法?
Any ideas to force CloudFlare to serve the html pages from their cache?
PS:我在图像上得到了CF-Cache-Status: HIT
,它可以正常工作,但在html页面上什么也没有,甚至没有CF-Cache-Status: something
.使用适用于html页面的CloudFlare页面规则,它似乎可以正常工作,但我想避免使用它,这主要是因为它太特定于CloudFlare了.我没有从这些页面提供Cookie或任何动态信息.
PS: I am getting the CF-Cache-Status: HIT
on the images and it works fine but on the html pages nothing, not even CF-Cache-Status: something
. With a CloudFlare page rule for html pages, it seems to work fine but I want to avoid using one, mainly because it's too CloudFlare specific. I am not serving cookies or anything dynamic from these pages.
推荐答案
现在可以让Cloudflare尊重您的Web服务器标头,而不用浏览器缓存TTL设置中描述的最小值覆盖它们.
It is now possible to get Cloudflare to respect your web servers headers instead of overriding them with the minimum described in the Browser Cache TTL setting.
首先导航到Cloudflare仪表板中的缓存"选项卡:
Firstly navigate to the Caching tab in the Cloudflare dashboard:
从这里您可以向下滚动到浏览器缓存过期"设置,从这里您可以在下拉列表中选择检查现有标题":
From here you can scroll down to the "Browser Cache Expiration" setting, from here you can select the "Respect Existing Headers" option in the dropdown:
进一步阅读:
- Does CloudFlare honor my Expires and Cache-Control headers for static content?
- Caching Anonymous Page Views
- How do I cache static HTML?
注意:如果未选择此设置,则Cloudflare将对Cache-Control标头至少应用默认的4小时.设置此设置后,Cloudflare将不会触摸您的Cache-Control标头(即使它们太低或根本没有设置).
Note: If this setting isn't chosen, Cloudflare will apply a default 4 hour minimum to Cache-Control headers. Once this setting is set, Cloudflare will not touch your Cache-Control headers (even if they're low or not at all set).
这篇关于CloudFlare上不遵守缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!