如何在IIS7中为每个文件夹和扩展配置静态内容缓存? [英] How to configure static content cache per folder and extension in IIS7?
问题描述
我想在IIS7中为我的ASP.NET网站中的静态内容缓存设置规则。
I would like to set up rules in IIS7 for static content caching in my ASP.NET website.
我看过这些文章,详细介绍了如何操作使用 web.config
中的< clientCache />
元素:
I have seen these articles, which details how to do it using the <clientCache />
element in web.config
:
Client Cache
<clientCache>
(IIS.NET)
Add Expires or Cache Control Header to static content in IIS (Stack Overflow)
但是,此设置似乎全局适用于所有静态内容。有没有办法只为某些目录或扩展名执行此操作?
However, this setting appears to apply globally to all static content. Is there a way to do this just for certain directories or extensions?
例如,我可能有两个需要单独缓存设置的目录:
For example, I may have two directories which need separate cache settings:
/ static / images
/ content / pdfs
是否可以设置发送缓存标头的规则( max-age
,到期
等)基于扩展名和文件夹路径?
Is it possible to set up rules for sending cache headers (max-age
, expires
, etc) based on extensions and folder paths?
请注意,我必须能够通过 web.config
执行此操作,因为我无法访问IIS控制台。
Please note, I must be able to do this via web.config
because I don't have access to the IIS console.
推荐答案
您可以在根 web.config
中为整个文件夹设置特定的缓存标头:
You can set specific cache-headers for a whole folder in either your root web.config
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Note the use of the 'location' tag to specify which
folder this applies to-->
<location path="images">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
</staticContent>
</system.webServer>
</location>
</configuration>
或者您可以在 web.config中指定这些
内容文件夹中的文件:
Or you can specify these in a web.config
file in the content folder:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
</staticContent>
</system.webServer>
</configuration>
我不知道有针对特定文件类型的内置机制。
I'm not aware of a built in mechanism to target specific file types.
这篇关于如何在IIS7中为每个文件夹和扩展配置静态内容缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!