启用浏览器缓存静态资源

2024-04-15

为了提高站点性能,我在 IIS 7.5 中添加了以下 http 标头。

Expires: Sun, 29 Mar 2020 00:00:00 GMT

and

Cache-Control: Public

我添加这些标题是为了images站点虚拟目录中的文件夹。 当我访问该网站时,我会看到该文件夹​​中存在的每个图像;这些响应标头是:

Accept-Ranges:bytes
Cache-Control:no-cache, no-store,Public
Content-Length:4445
Content-Type:image/png
Date:Fri, 06 Jun 2014 09:18:36 GMT
ETag:"16874c2af55ecf1:0"
Expires:-1,Sun, 29 Mar 2020 00:00:00 GMT
Last-Modified:Wed, 23 Apr 2014 13:08:48 GMT
max-age:604800
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET

我需要浏览器从缓存中获取这些图像,而不是再次向服务器请求。我应该如何实现它?


您的标头显示您添加了新值,但需要替换现有值

Cache-Control:no-cache, no-store,Public
Expires:-1,Sun, 29 Mar 2020 00:00:00 GMT

no-cache, no-store代表没有缓存并且-1说内容已经过期了。

您可以轻松地在根 web.config 文件中将其设置为

  <location path="images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseExpires" 
                     httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" /> 
      </staticContent>
    </system.webServer>
  </location>
</configuration>

其中 images 是目录的名称

或者直接在目标目录中添加专用的web.config文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseExpires" 
                   httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" /> 
    </staticContent>
  </system.webServer>
</configuration>

您还可以使用cacheControlMode =“UseMaxAge”并设置特定的过期时间

设置 7 天后过期的示例

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

阅读更多http://msdn.microsoft.com/en-us/library/ms689443.aspx http://msdn.microsoft.com/en-us/library/ms689443.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

启用浏览器缓存静态资源 的相关文章