HTTP 标头 - 缓存问题
我正在向图像发出请求,我得到的响应标头是:
Accept-Ranges:bytes
Content-Length:4499
Content-Type:image/png
Date:Tue, 24 May 2011 20:09:39 GMT
ETag:"0cfe867f5b8cb1:0"
Last-Modified:Thu, 20 Jan 2011 22:57:26 GMT
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
请注意缺少 Cache-Control
标头。
在 Chrome 上的后续请求中,Chrome 知道去缓存检索图像。它怎么知道要使用缓存呢?我的印象是我必须用 Cache-Control 标头来告诉它。
I am making a a request to an image and the response headers that I get back are:
Accept-Ranges:bytes
Content-Length:4499
Content-Type:image/png
Date:Tue, 24 May 2011 20:09:39 GMT
ETag:"0cfe867f5b8cb1:0"
Last-Modified:Thu, 20 Jan 2011 22:57:26 GMT
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
Note the absence of the Cache-Control
header.
On subsequent requests on Chrome, Chrome knows to go to the cache to retrieve the image. How does it know to use the cache? I was under the impression that I would have to tell it with the Cache-Control
header.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您同时拥有
ETag
和Last-Modified
标头。它可能会使用这些。但要做到这一点,它仍然需要分别使用If-None-Match
或If-Modified-Since
发出请求。You have both an
ETag
and aLast-Modified
header. It probably uses those. But for that to happen, it still needs to make a request withIf-None-Match
orIf-Modified-Since
respectively.要设置缓存控制,您必须自己指定它。您可以在 web.config 、IIS 管理器中对选定的文件夹(静态、图像...)执行此操作,也可以在代码中进行设置。 HTTP 1.1 标准建议未来一年作为最长过期时间。
对于站点中的所有静态内容,将到期日期设置为未来一年被认为是良好的做法。标头中没有它会导致
If-Modified-Since
请求,该请求可能比首次请求小型静态文件需要更长的时间。在这些调用中使用 ETag 标头。当您有
Cache-Control: max-age=315360000
时,基本 HTTP 响应将超过If-Modified-Since>
调用,因此最好删除 ETag 标头和导致更小的静态文件响应标头。 IIS 没有这方面的设置,因此您必须在OnPreServerRequestHeaders()
中执行response.Headers.Remove("ETag");
如果您想优化标头进一步,您可以删除 IIS 设置中的
X-Powered-By:ASP.NET
和 web 中的X-Aspnet-Version
标头(尽管我在您的回复中没有看到) .config - system.web/httpRuntime 元素中的enableVersionHeader="false"
。有关更多提示,我建议您阅读好书 - http://www.amazon.com/Ultra-快速 ASP-NET-Build-Ultra-Scalable-Server/dp/1430223839
To set the Cache-Control You have to specify it yourself. You can either do it in web.config , IIS Manager for selected folders (static, images ...) or set it in code. The HTTP 1.1 standard recommends one year in future as the maximum expiration time.
Setting expiration date one year in future is considered good practice for all static content in your site. Not having it in headers results in
If-Modified-Since
requests which can take longer then first time requests for small static files. In these calls ETag header is used.When You have
Cache-Control: max-age=315360000
basic HTTP responses will outnumberIf-Modified-Since>
calls and because of that it is good to remove ETag header and result in smaller static file response headers. IIS doesn't have setting for that so You have to doresponse.Headers.Remove("ETag");
inOnPreServerRequestHeaders()
And if You want to optimize Your headers further You can remove
X-Powered-By:ASP.NET
in IIS settings andX-Aspnet-Version
header (altough I don't see in Your response) in web.config -enableVersionHeader="false"
in system.web/httpRuntime element.For more tips I suggest great book - http://www.amazon.com/Ultra-Fast-ASP-NET-Build-Ultra-Scalable-Server/dp/1430223839