【问题标题】:IIS 7.5 and images not being cachedIIS 7.5 和未缓存的图像
【发布时间】:2012-05-26 01:58:28
【问题描述】:

我无法将图像文件缓存起来。我已经尝试了在此站点和其他站点上找到的所有内容,但仍然无法将它们缓存。

我尝试过的网络配置设置

    <staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
    </staticContent>
    <httpProtocol allowKeepAlive="true" />


    <caching enabled="true" enableKernelCache="true">
    <profiles>
    <add extension=".png" policy="CacheUntilChange" />
    <add extension=".jpg" policy="CacheForTimePeriod" duration="12:00:00" />
    </profiles>
    </caching>  

这是其中 1 张图片的响应标头

    Key Value
    Response    HTTP/1.1 200 OK
    Cache-Control   no-cache
    Content-Type    image/png
    Last-Modified   Thu, 16 Dec 2004 18:33:28 GMT
    Accept-Ranges   bytes
    ETag    "a1ca4bc9de3c41:0"
    Server  Microsoft-IIS/7.5
    X-Powered-By    ASP.NET
    Date    Fri, 18 May 2012 13:21:21 GMT
    Content-Length  775

【问题讨论】:

  • IIS 默认缓存静态内容。检查标头时,请确保您的开发工具没有设置禁用缓存的选项。

标签: iis caching iis-7.5 browser-cache


【解决方案1】:

以下应该会导致浏览器缓存您的图像:

<staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
    <customHeaders>
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

&lt;caching&gt;...&lt;/caching&gt; 块用于服务器端缓存,而不是客户端缓存。

【讨论】:

  • 这是否会在每个静态文件(.js、.css、.jpg 等)上启用客户端缓存标头?如何控制扩展?谢谢
  • 它启用了文件夹(和子文件夹)中所有静态内容(在 IIS 中启用)的缓存标头,并启用了 web.config 文件。因此,如果您的图像位于/images 中,则应使用上述内容创建web.config 并将其放在/images 文件夹中,这样只有图像会受此影响。您不能按文件扩展名启用/禁用它。
  • @Digerkam 500.19 错误表示配置错误。确保 web.config 是有效的 XML 文件。检查任何缺少的打开或关闭标签,并确保该块位于&lt;configuration&gt;&lt;system.webServer&gt;...&lt;/system.webServer&gt;&lt;/configuration&gt;
  • this 网站说&lt;caching&gt;...&lt;/caching&gt; 是客户端,现在谁是对的?你觉得呢?
  • @storm 我相信您链接到的页面上有一些错误信息。如果您按照他的步骤操作,则内​​容尚未(尚未)缓存在服务器上,正如他在第 2 步结束时所声称的那样。当您添加第 3 步中的行时,它确实缓存在服务器上(在用户空间)。 location="Client" 只在响应中添加Cache-Control: private 以防止它被缓存在代理服务器中。然而,浏览器的缓存是由Cache-Control 标头中的max-age 触发的。
【解决方案2】:

如果有人需要将您的网站配置为 Chrome 审核或 GTMetrix 要求,我已经使用以下内容配置了我的环境(感谢 Marco 的 answer):

<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />

使用365 days 并且两个工具都将该值视为缓存时间可接受的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    相关资源
    最近更新 更多