.net 向 IIS 中的静态内容添加过期或缓存控制标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/865252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:42:13  来源:igfitidea点击:

Add Expires or Cache Control Header to static content in IIS

.nethttpiiscaching

提问by Brian Grinstead

After running the YSlow pluginon a site, I saw that one of the recommendationswas to add far future expires headers to the scripts, stylesheets, and images.

在网站上运行YSlow 插件后,我看到其中一项建议是向脚本、样式表和图像添加远期过期标头。

I would like to do this, does anyone have experience with this? I am using IIS 7 and I read an article from Microsoftbut am not interested in disabling caching for asp pages or images, I actually want to force caching for static content. Also, the ideal situation would not exist in code, but in the web server configuration.

我想这样做,有人有这方面的经验吗?我正在使用 IIS 7,我阅读了Microsoft一篇文章,但对禁用 asp 页面或图像的缓存不感兴趣,我实际上想强制缓存静态内容。此外,理想情况不存在于代码中,而是存在于 Web 服务器配置中。

So, what steps would I have to take to have every image, javascript file, and stylesheet to be cached?

那么,我必须采取哪些步骤才能缓存每个图像、javascript 文件和样式表?

回答by Vladislav

Can this be done in IIS 6?

这可以在 IIS 6 中完成吗?

To configure content expiration

配置内容过期

  1. In the Internet Information Services (IIS) Manager administrative tool, right-click Your Web Site, and then click Properties.

  2. In the Properties dialog box, on the HTTP Headers tab specify expiration time, and then click OK.

  1. 在 Internet 信息服务 (IIS) 管理器管理工具中,右键单击您的网站,然后单击属性。

  2. 在“属性”对话框中,在“HTTP 标头”选项卡上指定到期时间,然后单击“确定”。

回答by v1rich

I added a custom header on the http headers tab instead of checking the box, because I could not include "public" in the Cache Control header otherwise.

我在 http 标头选项卡上添加了一个自定义标头,而不是选中该框,因为否则我无法在缓存控制标头中包含“public”。

I did this for folders with static content such as /images/, /scripts/, /css/ and a few others, not the whole project.

我为包含静态内容的文件夹(例如 /images/、/scripts/、/css/ 和其他一些文件夹,而不是整个项目)执行此操作。

Name: Cache-Control Value: public, max-age=604800

名称:Cache-Control 值:public, max-age=604800

VR

虚拟现实