首页 文章

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

提问于
浏览
137

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

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

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

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

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

/ static / images / content / pdfs

是否可以根据扩展名和文件夹路径设置发送缓存头( max-ageexpires 等)的规则?

请注意,我必须能够通过 web.config 执行此操作,因为我无法访问IIS控制台 .

3 回答

  • 64

    您可以在根 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 文件中指定它们:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <staticContent>
          <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
        </staticContent>
      </system.webServer>
    </configuration>
    

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

  • 210

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

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <location path="YourFileNameHere.xml">
            <system.webServer>
                <staticContent>
                    <clientCache cacheControlMode="DisableCache" />
                </staticContent>
            </system.webServer>
        </location>
    </configuration>
    
  • -1

    我有同样的问题 . 对我来说,问题是如何为图像配置缓存限制 . 我遇到了这个网站,它提供了一些有关如何处理问题的程序的见解 . 希望它对你也有帮助:[https://varvy.com/pagespeed/cache-control.html]

相关问题