如何在IIS7中为每个文件夹和扩展配置静态内容缓存?

fra*_*lic 140 iis iis-7 caching web-config http-headers

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

我已经看过这些文章,详细介绍了如何使用以下<clientCache />元素web.config:

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

但是,此设置似乎全局适用于所有静态内容.有没有办法只为某些目录或扩展这样做?

例如,我可能有两个需要单独缓存设置的目录:

/static/images
/content/pdfs

是否有可能建立规则发送缓存头(max-age,expires基于扩展和文件夹路径等)?

请注意,我必须能够这样做,web.config因为我无法访问IIS控制台.

Kev*_*Kev 212

您可以在根目录中为整个文件夹设置特定的缓存标头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>
Run Code Online (Sandbox Code Playgroud)

或者您可以web.config在内容文件夹中的文件中指定这些:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
    </staticContent>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我不知道内置机制来定位特定的文件类型.

  • @StuffandBlah是的,它是递归的.我自己尝试过,IIS将相同的缓存控制设置应用于我指定为"位置"的文件夹的子文件夹中的所有文件请求. (9认同)
  • 有谁知道这是递归的吗?例如,如果你的图像路径下有子文件夹,它还会缓存那些吗? (7认同)
  • @vtortola - [IIS7资源工具包](:http://www.amazon.co.uk/dp/0735624410)不会出错,它实际上非常有用.[Wrox Pro IIS7](http://www.amazon.co.uk/dp/0470152532)书也不错.TBH我主要从IIS.NET配置参考站点了解到:http://www.iis.net/ConfigReference,以及关于`%systemroot%\ system32\inetsrv\config\applicationhost.config`文件和相关朋友. (5认同)

Jef*_*tis 67

您可以基于每个文件执行此操作.使用path属性包含文件名

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <location path="YourFileNameHere.xml">
        <system.webServer>
            <staticContent>
                <clientCache cacheControlMode="DisableCache" />
            </staticContent>
        </system.webServer>
    </location>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • @Zulgrib将其应用于特定扩展,您可以使用出站重写规则:http://stackoverflow.com/questions/32987486/iis-setting-cache-control-header-per-file-type/33444897#33444897 (2认同)