【问题标题】:IIS7 cacheControlMaxAge attribute not workingIIS7 cacheControlMaxAge 属性不起作用
【发布时间】:2012-09-29 00:45:26
【问题描述】:

在 IIS 7.5 中,我将 cacheControlMaxAge 设置为一年

<location path="Content/Images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
      </staticContent>
    </system.webServer>
  </location>

根据本指南:Setting Expires and Cache-Control: max-age headers for static resources in ASP.NET

但是,Google PageSpeed 工具仍然说文件没有被缓存:

The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources:
* https://local.example.com/Content/Images/image1.png (expiration not specified)
(etc)

为什么会显示“未指定有效期”?

整个 webapp 都通过 https 提供服务,这是一个因素吗?

【问题讨论】:

  • 您是否检查了 image1.png 的 http 标头?您可以在浏览器的开发工具(Chrome/FF/IE 中的 F12)中的网络选项卡下执行此操作。或者你可以使用提琴手。如果设置了最大年龄,所有现代浏览器都会通过 https 缓存项目。
  • http 标头说Cache-Control: public,但没有提及到期,到期标头应该叫什么?
  • 已解决:将缓存属性添加到location path="Content" 而不是location path="Content/Images" 解决了问题。
  • 如果您已经解决了问题,您能否将答案发布为答案?

标签: asp.net-mvc caching https iis-7.5 browser-cache


【解决方案1】:

我通过将指定的路径从 Content/Images 更改为 Content 解决了这个问题

<location path="Content">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlCustom="public" 
                     cacheControlMode="UseMaxAge" 
                     cacheControlMaxAge="365.00:00:00" />
      </staticContent>
    </system.webServer>
  </location>

所以它是固定的,但是路径的改变并不能说明问题到底是什么。

【讨论】:

    【解决方案2】:

    我发现 Google PageSpeed 在某些情况下需要一些时间才能“赶上”您最近所做的更改。确保您已完成整页刷新并点击 PageSpeed 本身的刷新按钮。如果做不到这一点,在 Firefox 上使用 Firebug 似乎总是在 net 选项卡中给出准确的结果。单击文件旁边的加号图标并检查响应标头。

    【讨论】:

    • 我发现在页面末尾添加一个不相关的查询字符串会强制 Google PageSpeed 获取最新版本。即example.com/testpage.htm?x=1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2011-03-31
    • 2018-11-19
    • 2012-07-23
    • 2016-07-16
    • 2020-01-24
    • 2015-09-05
    相关资源
    最近更新 更多