为什么浏览器仍然使用 max-age 发送缓存控制公共请求? [英] Why does Browser still sends request for cache-control public with max-age?

查看:31
本文介绍了为什么浏览器仍然使用 max-age 发送缓存控制公共请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Amazon S3 对象,并且为每个对象设置了

I have Amazon S3 objects, and for each object, I have set

Cache-Control: public, max-age=3600000

大约是 41 天.

我将 Amazon CloudFront Distribution 设置为最小 TTL,也设置为 3600000.

And I have Amazon CloudFront Distribution set with Minimum TTL also with 3600000.

这是清除缓存后的第一个请求.

This is the first request after clearing cache.

GET /1.0.8/web-atoms.js HTTP/1.1
Host: d3bhjcyci8s9i2.cloudfront.net
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

响应是

HTTP/1.1 200 OK
Content-Type: application/x-javascript
Content-Length: 226802
Connection: keep-alive
Date: Wed, 28 Aug 2013 10:37:38 GMT
Cache-Control: public, max-age=3600000
Last-Modified: Wed, 28 Aug 2013 10:36:42 GMT
ETag: "124752e0d85461a16e76fbdef2e84fb9"
Accept-Ranges: bytes
Server: AmazonS3
Age: 342557
Via: 1.0 6eb330235ca3971f6142a5f789cbc988.cloudfront.net (CloudFront)
X-Cache: Hit from cloudfront
X-Amz-Cf-Id: 92Q2uDA4KizhPk4TludKpwP6Q6uEaKRV0ls9P_TIr11c8GQpTuSfhw==

即使亚马逊明确发送了 Cache-Control,Chrome 仍然会发出第二个请求,而不是从 Cache 中读取.

Even while Amazon clearly sends Cache-Control, Chrome still makes second request instead of reading it from Cache.

GET /1.0.8/web-atoms.js HTTP/1.1
Host: d3bhjcyci8s9i2.cloudfront.net
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "124752e0d85461a16e76fbdef2e84fb9"
If-Modified-Since: Wed, 28 Aug 2013 10:36:42 GMT

问题:为什么 chrome 会提出第二个请求?

Question: Why does chrome makes second request?

过期当我在标题中放置显式 Expires 属性时,这种行为会发生变化.浏览器不会发送对 Expires 标头的后续请求,但对于缓存控制公共,它会发送它.我所有的 S3 对象永远不会改变,它们是不可变的,当我们更改文件时,我们将它们作为具有新 URL 的新对象.

Expires This behavior changes when I put an explicit Expires attribute in headers. Browser will not send subsequent request for Expires header, but for cache-control public, it does send it. My all S3 objects will never change, they are immutable, when we change file, we put them as new object with new URL.

页面内脚本参考Chrome 仅有时会发出后续请求,我通过在浏览器中实际输入 URL 来进行此测试.当 HTML 页面引用脚本时,对于少数后续请求,chrome 会加载缓存的脚本,但在一段时间后再次向服务器发送请求.这里没有磁盘大小问题,Chrome 有足够的缓存空间.

In Page Script Reference Chrome makes subsequent requests only sometimes, I did this test by actually typing URL in browser. When script is referenced by HTML page, for few subsequent requests chrome loads cached scripts, but once again after sometime, once in a while it does send request to server. There is no Disk Size issue here, Chrome has sufficient cache space.

问题是我们为每个请求收费,我希望 S3 对象被永久缓存,并且应该从缓存加载并且永远不应该连接到服务器.

Problem is we get charged for every request, and I want S3 objects to be cached forever, and should be loaded from Cache and should never connect to server back.

推荐答案

如果 HTTP 响应包含 etag 条目,则将始终发出条件请求.ETag 是一个缓存验证器标签.客户端总是会向服务器发送 etag 以查看该元素是否已被修改.

If the HTTP Response contains the etag entry, the conditional request will always be made. ETag is a cache validator tag. The client will always send the etag to the server to see if the element has been modified.

这篇关于为什么浏览器仍然使用 max-age 发送缓存控制公共请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆