如何在缓存控制策略中排除单个图像或一组图像?

huangapple go评论61阅读模式
英文:

How does one exclude a single image, or a set of images from a cache control policy?

问题

我想在我的网站上为一些图片设置缓存控制策略,以满足谷歌的要求,但我不希望网站上的所有图片都被缓存。尽管我对缓存控制策略的工作原理有些模糊,但我理解的是,我将它放在页面的标头中,页面上的所有图片、JS和CSS都会被缓存,缓存时间由指定的时间确定。

但如果我将缓存控制策略设置为:

Cache-Control: Max-age=31536000

我该如何阻止特定的资产在那一年的时间内被缓存。我的页面上有一些横幅和图片,它们定期更新,我不能让它们被缓存并保持不变一整年。

英文:

I'm looking to set a cache control policy on some of the images on my website to appease the Google lords, however I don't want all the images on my website to be cached. While I'm a bit fuzzy on how the Cache control policy works, my understanding is that I place it in the header of my page, and all the images, JS, and CSS on the page gets cached for the set amount of time specified.

But if I set my cache control policy to:

Cache-Control: Max-age=31536000

How do I keep a specific asset or assets from being cached for that years time. I have banners and images on my page that are updated every so often that I can't have cached and left static for a years time.

答案1

得分: 1

不,缓存标头仅适用于返回的特定响应,不适用于页面链接到的任何其他资源。所有其他资源需要它们自己的缓存标头,这当然允许您为每个资源选择特定的缓存时间。

英文:

While I'm a bit fuzzy on how the Cache control policy works, my understanding is that I place it in the header of my page, and all the images, js, and css on the page gets cached for the set amount of time specified.

No, cache headers only apply to the specific response being returned, not any other resources linked to by the page. All the other assets need their own cache headers, which of course allows you to choose a specific cache time for each resource.

答案2

得分: 0

为什么不想缓存图像?图像是静态对象;如果你创建不同的图像,它们应该有不同的名称或扩展类型。

话虽如此,如果你使用 CDN,大多数都允许在资源上配置自定义 TTL。我建议联系 CDN 支持以了解如何设置规则。

此外,大多数常见的应用程序(Rails、Express 等)和 Web 服务器(Nginx、Apache)也允许覆盖标头。与 CDN 选项相比,服务器端配置可能更灵活,因为你控制逻辑,而不依赖于第三方控制。

这里是关于缓存控制标头的 链接,来自 Mozilla 的规范。

英文:

Why don't you want to cache images? Images at static objects; if you're creating different images then they should have different names, or extension types.

With that being said, if you're using a a CDN, most allow for configuration of custom TTLs on resources. I'd recommend reaching out to CDN support to figure out how to set up rule.

Also, most common applications (Rails, Express, etc) and Web Servers (Nginx, Apache) allow for overriding the headers as well. Server side configuration may be more flexible then the CDN option since you control the logic and not dependent on a third party controlling it.

Here's a link to Mozilla's spec on the cache-control header

huangapple
  • 本文由 发表于 2023年3月7日 23:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定