如何在 IIS7 中为每个文件夹和扩展配置静态内容缓存? [英] How to configure static content cache per folder and extension in IIS7?

查看:25
本文介绍了如何在 IIS7 中为每个文件夹和扩展配置静态内容缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 IIS7 中为我的 ASP.NET 网站中的静态内容缓存设置规则.

I would like to set up rules in IIS7 for static content caching in my ASP.NET website.

我看过这些文章,其中详细介绍了如何使用 <clientCache/> 元素在 web.config 中进行操作:

I have seen these articles, which details how to do it using the <clientCache /> element in web.config:

客户端缓存 (IIS.NET)
在静态内容中添加过期或缓存控制标头IIS(堆栈溢出)

但是,此设置似乎全局适用于所有静态内容.有没有办法仅针对某些目录或扩展程序执行此操作?

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-ageexpires 等)的规则?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆