简体   繁体   English

如何正确设置Amazon CloudFront缓存

[英]How to properly setup Amazon CloudFront caching

I'm using S3 to store a bunch of images and CloudFront to cache them. 我正在使用S3存储一堆图像,并使用CloudFront缓存它们。 The problem I'm getting is that my images are not being cached correctly. 我遇到的问题是我的图像未正确缓存。

If I check the headers for an image through the CloudFront link, it will show a MISS on the first try and HIT for all subsequent tries. 如果我通过CloudFront链接检查图像的标题,它将在第一次尝试中显示MISS,并在所有后续尝试中显示HIT。 This is normal, but if I close my browser, go away for a few hours and try it again it will show a MISS. 这是正常现象,但是如果我关闭浏览器,请离开几个小时然后再试一次,它将显示MISS。 Therefore indicating that it's only getting cached for short period. 因此,表明它仅在短时间内被缓存。

Here's a sample image on the cdn: http://d711ds9nsj32q.cloudfront.net/static/8e1f8567e229e24e0782ed_1e994b9dc56ba092ecd2_5.jpg 这是CDN上的示例图片: http ://d711ds9nsj32q.cloudfront.net/static/8e1f8567e229e24e0782ed_1e994b9dc56ba092ecd2_5.jpg

A weird behavior is that if you try to sniff the headers with web-sniffer.net you'll see a MISS every single time. 怪异的行为是,如果您尝试使用web-sniffer.net嗅探标头,则每次都会看到MISS。 Even though the cache control is public, max-age=315360000 and it's set to expire after 10 years. 即使缓存控制是public, max-age=315360000并设置为10年后过期。

What is the proper way to cache something for 10 years on cloudfront? 在Cloudfront上缓存10年的正确方法是什么? Are there any extra headers I'm missing? 我还缺少其他标题吗?

Edit : For comparison here's the same image on imgur. 编辑 :为了进行比较,这是imgur上的相同图像 They use cloudflare but the behavior I see there is correct. 他们使用cloudflare,但是我看到的行为是正确的。 Once it caches, I never see a miss again from any of my computers or nearby locations. 缓存完成后,我再也不会从任何计算机或附近位置看到任何丢失的消息。 Both my headers and theirs are similar except for a few (unimportant) differences. 除了一些(不重要)差异之外,我的标头及其标题均相似。 Any help is appreciated. 任何帮助表示赞赏。

It could be that you have minimum TTL set to something non-default and it's caching it for that time period instead of whatever's in your Cache-Control header. 可能是您已将最小TTL设置为非默认值,并在该时间段内对其进行缓存,而不是在Cache-Control标头中进行缓存。 I think that's unlikely both because you probably already thought of it and because it appears the distribution respects Cache-Control: no-cache (and it wouldn't if minimum TTL was non-default). 我认为这不太可能,因为您可能已经考虑过它,并且因为它似乎在发行版中遵守了Cache-Control:no-cache(如果最小TTL是非默认值,则不会)。

More likely, cloudfront is just evicting your items after a while: 更有可能的是,Cloudfront只是在一段时间后将您的物品逐出:

If an object in an edge location isn't frequently requested, CloudFront might evict the object—remove the object before its expiration date—to make room for objects that are more popular. 如果不经常请求位于边缘位置的对象,则CloudFront可能会驱逐该对象(在其到期日期之前删除该对象),以便为更受欢迎的对象腾出空间。

- from this article -来自本文

As for web-sniffer.net, you'll notice that it sends a Cache-Control: no-cache header, which will make cloudfront fetch from origin. 至于web-sniffer.net,您会注意到它发送了Cache-Control:no-cache标头,这将使Cloudfront从源头获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM