将静态文件(图像等)的缓存有效期设置为几个月或几年,有必要吗?

发布于 2024-12-05 05:57:00 字数 964 浏览 2 评论 0原文

1.我正在使用一些 .htaccess 代码为我的服务器上的图像设置缓存到期期限。问题是,当我检查一些流行网站上图像的缓存到期期限时,它大多在 31536000(1 年)之间)15552000(180 天)5184000(60 天)。缓存过期标头用于告诉浏览器将静态文件存储在缓存中一段时间​​,对吗?然后呢?!为什么这些网站要设置这么长的有效期?我在这里错过了什么吗?

2.另外,您认为以下哪一个(.htaccess 代码)更好?

<FilesMatch "\.(ico|jpg|jpeg|png|gif|swf|pdf)$">
ExpiresDefault A0
Header set Cache-Control "max-age=5184000,  public, must-revalidate, proxy-revalidate"
</FilesMatch>

<--- 或 --->

# Expire images header
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A5184000
ExpiresByType image/png A5184000
ExpiresByType image/jpg A5184000
ExpiresByType image/jpeg A5184000
ExpiresByType image/ico A5184000
ExpiresByType image/swf A5184000
ExpiresByType image/pdf A5184000

编辑:3. 如何设置到期-外部文件的标头? 例如,来自外部源的 js 和 css 文件? (我检查过,许多没有设置缓存有效期)

寻找知识渊博的答案。谢谢。

1. I am setting cache expiry period for images on my server using some .htaccess code. The thing is, when I checked the cache-expiry period of images on some popular websites, it's mostly between 31536000 (1 year) to 15552000 (180 days) to 5184000 (60 days). A cache-expiry header is used to tell the browser to store the static files in its cache for a certain period, right? Then WTH?! Why are these websites setting such long expiry? Am I missing something here?

2. Also which of these below (.htaccess codes) is better in your opinion?

<FilesMatch "\.(ico|jpg|jpeg|png|gif|swf|pdf)$">
ExpiresDefault A0
Header set Cache-Control "max-age=5184000,  public, must-revalidate, proxy-revalidate"
</FilesMatch>

<--- OR --->

# Expire images header
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A5184000
ExpiresByType image/png A5184000
ExpiresByType image/jpg A5184000
ExpiresByType image/jpeg A5184000
ExpiresByType image/ico A5184000
ExpiresByType image/swf A5184000
ExpiresByType image/pdf A5184000

EDIT: 3. How to set expiry-headers for external files? for example, js and css files from external sources? (many do not have a set cache expiry period, I checked)

Looking for a knowledgeable answer. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2024-12-12 05:57:00

我不确定我能否准确回答您的问题,但是这个链接描述了您的内容正在追赶。

您也可以在第二个示例中为 js 和 css 文件设置缓存控制。根据答案,第二种格式更好,因为它不依赖于文件扩展名以及文章中解释的其他内容。

关于较长的过期时间,我猜这是网站管理员的选择,但我还遇到了文章中未提及的另一个选项:Cache Control: Public,这本质上是为了强制存储您的图像据我所知,在世界各地的 ISP 服务器上。也许较长的到期时间是完成类似任务的较旧技术。

I'm not sure I can answer your question exactly, but this link describes what you're after.

You can set cache-control on your second example for js and css files too. According to the answer the second format is better because it is not dependent on the file extension, and other things explained in the article.

Regarding long expiry times, that's the choice of the webmaster I guess but I also came across another option which is not mentioned in the article: Cache Control: Public which is essentially meant to force your images to be stored on ISPs servers all over the world, as far as I can see. Perhaps the long expiry times are older tech to accomplish something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文