`
fantaxy025025
  • 浏览: 1279169 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

200 OK (from cache) 与 304 Not Modified

 
阅读更多

 

200 OK (from cache) 与 304 Not Modified

https://www.bokeyy.com/post/200-ok-from-cache-vs-304-not-modified.html要看博主原文和评论吧,很不错。

另外博主原文中有一系列前端优化的记录,很适合前端性能方面有待优化的,好好看看。

 

为什么有的缓存是 200 OK (from cache),有的缓存是 304 Not Modified 呢?很简单,看运维是否移除了 Entity Tag。移除了,就总是 200 OK (from cache)。没有移除,就两者交替出现。

最近在做百度云观测的 nginx 配置优化。从知乎上看到这个问题:“阿里云存储如何让浏览器始终以200 (from cache)缓存图片?”,提问者强调 200 OK (from cache) 和 304 Not Modified 的区别,有感而发。

其实, 200 OK (from cache)  是浏览器没有跟服务器确认,直接用了浏览器缓存;而 304 Not Modified 是浏览器和服务器多确认了一次缓存有效性,再用的缓存。

它们都是在设置了缓存的情况下触发的。

那么,两者触发的时机有什么区别呢?200 OK (from cache) 是直接点击链接访问,输入网址按回车访问也能触发;而 304 Not Modified 是刷新页面时触发,或是设置了长缓存、但 Entity Tags 没有移除时触发。这是经过查阅资料得出的结论。博主实际测试了一下,结论与之相符:

直接访问有缓存的网站都触发 200 OK (from cache)图1 – 直接访问有缓存的网站都触发 200 OK (from cache)刷新浏览器会触发 304 Not Modified图2 – 刷新浏览器则会触发 304 Not Modified同一域名下,没有 Entity Tag 的资源直接访问,是 200 OK (from cache) 的结果图3 – 同一域名下,没有 Entity Tag 的资源直接访问,是 200 OK (from cache) 的结果同一域名下,有 Entity Tag ,直接访问就会触发 304 Not Modified图4 – 同一域名下,有 Entity Tag ,直接访问就会触发 304 Not Modified

现在一般都会设置长时间的缓存,正确设置方式参考这两篇笔记:

参考文献

后记

搜索了一下,发现这个问题,在网络上并还没有定论,也没发现有人去实测。

想想也对,现在网络那么快,304 Not Modified 还是 200 OK (from cache),如果不是较真地追求速度,可能大家都觉得区别不大,从而也就没发现这个问题了。

博主截图的域名是某著名 IT 公司的 CDN(已反馈)。可见犯这个错误的运维 GG 还真不少呢!

本文并不是说影响浏览器缓存只有 ETag 这一个因素的意思,请大家不要误解。只是就“为什么我加了缓存,有的却是 304 Not Modified, 而不是 200 OK(from cache)”这件事给出一个一针见血的原因和解答。本文也发在了 div.io 上,详细的 ETag 补充知识可以看 @hefangshi 补充的评论

 

1

1

1

 

3

3

3

分享到:
评论

相关推荐

    前端浏览器缓存及代码部署

    我前面写过浏览器缓存的文章,也写过html5离线缓存,关于这些缓存,我们很容易搞不清,例如:200 OK (FROM MemoryCache、FROM DiskCache) 及 304 NOT MODIFIED 还有 application cache (离线缓存)等等。 200 from me

    浏览器HTTP缓存机制

    如果没有过期,则直接从缓存中读取数据并返回200 OK (from cache)。 3. **重新验证**: 如果资源已经过期,浏览器将向服务器发送一个新的请求,附带`If-Modified-Since` 或 `If-None-Match` 头部,询问资源是否已被...

    前端大厂最新面试题-megvii.docx

    * 200 OK:请求成功 * 301 Moved Permanently:永久重定向 * 302 Found:临时重定向 * 404 Not Found:资源未找到 * 500 Internal Server Error:服务器内部错误 301和302的区别 301和302都是http状态码,都是用于...

    计算机网络第六版答案

    14. If the two ISPs do not peer with each other, then when they send traffic to each other they have to send the traffic through a provider ISP (intermediary), to which they have to pay for carrying ...

    acpi控制笔记本风扇转速

    not loaded (FADT, FACS, DSDT). BZ 477 Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually ...

    JLink_Windows_V648.zip

    DLL: Renesas RX231: OFS1 could not be modified. Fixed. DLL: Renesas RX: Added support for RX66N series devices DLL: Renesas RX: Added support for RX72M series devices DLL: Renesas RX: Added support ...

    vcpkg-master.zip

    Note: It might be necessary to delete the CMake cache folder of each modified configuration, to force a full regeneration. In the CMake menu, under Cache () you'll find Delete Cache Folders. Now ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    One exception is if Foo is used in myfile.cc, it's ok to #include (or forward-declare) Foo in myfile.h, instead of myfile.cc. Inline Functions link ▶Define functions inline only when they are ...

    曲线拟合工具CurveExpert 1.0

    from the cache instead of recalculating the curve fit. Fixed. + running two versions of CurveExpert is prevented, since two simultaneous instances cause stack faults. + if the data set is sorted,...

Global site tag (gtag.js) - Google Analytics