如何使 HTTP 缓存标头发挥作用

发布于 2024-09-11 20:05:34 字数 1140 浏览 2 评论 0原文

有人可以帮我解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-09-18 20:05:34

删除“上次修改”标头。如果该标头存在,某些浏览器总是发送条件 GET。

Remove "Last-Modified" header. Some browsers always send conditional GET if that header exists.

生生不灭 2024-09-18 20:05:34

难道是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文