简体   繁体   English

AWS Cloudfront和浏览器缓存设置

[英]AWS Cloudfront and browser cache settings

Hi so I have AWS Cloudront running on my site. 嗨,我的网站上运行着AWS Cloudront。 I also enabled browser caching through .htaccess file. 我还通过.htaccess文件启用了浏览器缓存。

Soon after I enabled browser caching my Cloudfront hits went very low and misses very high. 启用浏览器缓存后不久,我的Cloudfront命中率非常低,而错过率却很高。

My htaccess has following: 我的htaccess有以下内容:

ExpiresByType text/css "access plus 7 days" 
ExpiresByType application/javascript "access plus 7 days" 
ExpiresByType text/javascript "access plus 7 days" 
ExpiresByType application/x-javascript "access plus 7 days" 
ExpiresByType text/js "access plus 7 days" 
ExpiresByType text/x-javascript "access plus 7 days" 
ExpiresDefault "access plus 2 days"

Is this to be expected with browser caching and Cloudfront? 浏览器缓存和Cloudfront可以预期吗? Thanks! 谢谢!

Depending on your specific .htaccess configuration - this could very well cause what you've seen. 根据您的特定.htaccess配置-这很可能导致您所看到的。

Cloudfront Behaviour 云前行为

If your backend doesn't nominate any specific cache headers, then cloudfront will cache according to your cloudfront configuration - but as there are no cache headers, the browser will generally NOT cache, but will hit cloudfront on each request. 如果您的后端未指定任何特定的缓存头,则cloudfront将根据您的cloudfront配置进行缓存-但由于没有缓存头,浏览器通常不会缓存,但会在每次请求时访问cloudfront。 Cloudfront will record each as a HIT. Cloudfront会将每个记录为HIT。

If your backend does nominate cache headers, Cloudfront will respect them - but also will pass them onto the browser, so the browser itself may cache the object. 如果您的后端确实指定了缓存头,则Cloudfront将尊重它们-还将它们传递给浏览器,因此浏览器本身可能会缓存对象。 As such, the browser will only hit Cloudfront when the cache expires - but by that time, Cloudfront will also have expired its cache of the object - and will thus record it as a MISS. 这样,浏览器将仅在缓存过期时访问Cloudfront-但到那时,Cloudfront也将使对象的缓存过期-并将其记录为MISS。

See Specifying How Long Objects Stay in a Cloudfront Edge Cache and Specifying the Amount of Time that CloudFront Caches Objects for Web Distributions for more details on how Cloudfront and the browser handle different combination of origin headers. 有关Cloudfront和浏览器如何处理原始标头的不同组合的更多详细信息,请参阅指定对象在Cloudfront Edge缓存中保留 的时间指定CloudFront为Web分发缓存对象的时间

.htaccess Config .htaccess配置

Each of the ExpiresByType directives will cause a cache header Cache-Control: max-age=604800 to be added to each request (604800 is the number of seconds in 7 days) - which will signal to the browser that it can cache the object for that duration. 每个ExpiresByType指令都会导致将缓存标头Cache-Control: max-age=604800添加到每个请求中(604800是7天的秒数)-这将向浏览器发出信号,表明可以为其缓存对象持续时间。

See this Caching Tutorial for a more detailed explanation of the different headers you can use to effect caching and their impact on caches (eg Cloudfront) and browsers. 看到这个缓存教程你可以用它来实现缓存和他们的高速缓存(例如的Cloudfront)和浏览器的影响不同标题的更详细的解释。

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

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