如何使 HTTP 缓存标头发挥作用
有人可以帮我解决 HTTP 缓存问题吗?
我认为如果我设置“Expires”和“Cache-Control:max-age”请求标头,那么浏览器在 max-age 过去之前不会发出 GET 请求?
我的服务器看到大量条件 GET 请求,并以 304 进行回复。但是,如果可能的话,我想消除这些条件 GET,并且让浏览器仅在认为资源已过期时才请求资源。
以下是我从 Chrome 中的开发人员工具获得的详细信息:
Request URL:http://localhost:8080/img/branding.gif
Request Method:GET
Status Code:304 Not Modified
请求标头:
Cache-Control:max-age=0
If-Modified-Since:Thu, 22 Jul 2010 10:54:34 GMT
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4
响应标头:
Cache-Control:max-age=2246400, s-maxage=0
Connection:Keep-Alive
Content-Location:/img/branding.gif
Content-Type:image/gif
Date:Fri, 23 Jul 2010 15:06:10 GMT
Expires:Fri, 07 Jan 2011 15:06:10 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Thu, 22 Jul 2010 10:54:34 GMT
Pragma:cache
Server:Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)
我在 Java 代码的过滤器中设置了缓存标头。该代码在 Oracle 的 OC4J 上运行。
另外,我想通过 HTTPS 运行它。使用相同的标头也适用于 HTTPS 吗?
提前致谢。
Can someone please help me with a HTTP caching question?
I thought that if I set the "Expires" and "Cache-Control:max-age" request headers, then a browser wouldn't make a GET request until after the max-age had passed?
My server is seeing lots of conditional GET requests, and it replies with a 304. However, I'd like to eliminate these conditional GETs if possible and have the browser only ask for a resource once it thinks it has expired.
Here's the details I get from the developer tools in Chrome:
Request URL:http://localhost:8080/img/branding.gif
Request Method:GET
Status Code:304 Not Modified
Request Headers:
Cache-Control:max-age=0
If-Modified-Since:Thu, 22 Jul 2010 10:54:34 GMT
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4
Response Headers:
Cache-Control:max-age=2246400, s-maxage=0
Connection:Keep-Alive
Content-Location:/img/branding.gif
Content-Type:image/gif
Date:Fri, 23 Jul 2010 15:06:10 GMT
Expires:Fri, 07 Jan 2011 15:06:10 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Thu, 22 Jul 2010 10:54:34 GMT
Pragma:cache
Server:Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)
I set the cache headers in a filter in my Java code. The code is running on Oracle's OC4J.
Also, I want to run this over HTTPS. Will using the same headers work for HTTPS too?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除“上次修改”标头。如果该标头存在,某些浏览器总是发送条件 GET。
Remove "Last-Modified" header. Some browsers always send conditional GET if that header exists.
难道是s-maxage=0吗?您可以丢掉它,还是将其设置为与 maxage 相同吗?
条件获取可能来自代理,或者来自以某种方式决定充当代理的浏览器。我承认,这似乎不太可能。
Could it be the s-maxage=0? Can you lose that, or set it the same as the maxage?
The conditional gets could be coming from proxies, or from browsers that have somehow decided to act like proxies. Seems unlikely, i admit.