Web cache-related HTTP header information Detailed

Foreword

Li Zhihui teacher before reading the book "large-scale Web Site Technology Framework - Core Principles and Case Studies" This book, the book repeatedly mentioned the topic of the browser cache, which is just a few days of production have encountered a problem with the cache, found own book is no less look, serious heart's content does not have much to go, this time to work through the problems encountered, together with the HTTP header information associated with the web page summary of some of the cache, to a summary for yourself so the post-inspection; secondly put their own ideas and to share with friends in the garden, pointing to each other, so, not Miaozai! ! !

Simple conclusion, the HTTP-related pages cached HTTP header information divided into the following three groups:

  • Last-ModifiedwithIf-Modified-Since
  • ETagswithIf-None-Match
  • ExpireswithCache-Control

Here are three of these HTTP headers detailed description and summary!

Last-Modified和If-Modified-Since

In HTTP, Last-Modifiedthe If-Modified-SinceHTTP header information is used to record the page was last modified, the difference between the two is as follows:

  • Last-ModifiedIt is sent from the server to the client's HTTPhead;
  • If-Modified-SinceIt is first transmitted to the server by the client.

So, please keep in mind these basic knowledge, so as to work in the request packet analysis will not be confused and disoriented.

As shown above, when the user requests the browser first pages, the server returns a Last-Modified:Mon, 26 Apr 2019 13:22:17 GMTsuch a request header; when the user accesses the corresponding web page, the browser server response will Last-Modifiedassign the value If-Modified-Since, then, browsing It will take If-Modified-Since:Mon, 26 Apr 2019 13:22:17 GMTsuch a request to access the first server application. After the server receives the request, the time will update this page with the corresponding If-Modified-Sincefor comparison to determine whether to return 304 or 200 redirect code success code.

ETags和If-None-Match

By summarizing the above, we know Last-Modifiedand If-Modified-Sincecan only judge last modified resources, in order to decide whether to use the cache. While ETagsand If-None-Matchare more a little more advanced. Through ETagsand If-None-Matchwe can judge the resources of any property in order to determine whether to use the cache. Similarly, we also need to remember ETagsand If-None-Matchtwo knowledge points:

  • ETagsIt is sent from the server to the client's HTTPhead;
  • If-None-MatchIt is first transmitted to the server by the client.

And request logic Last-Modifiedand If-Modified-Sinceroughly the same, the difference is determined on the server side. For example, in some specific cases, some static files, it may be frequent updates, but the content of the document has not changed, this time if you use Last-modifiedthe server side always returns the latest content to the browser, which Etagis based on the file contents, if content has not changed, then the file will always use the local browser cache. Therefore, the use ETagmay be better to avoid unnecessary server accordingly.

Expires和Cache-Control

Add Expireshead can effectively use the browser's caching capabilities to improve the performance of the page, you can effectively avoid a lot of unnecessary HTTP requests on subsequent pages, WEB server using Expiresheader to tell the Web client that it can use a copy of the current assembly, until a specified time. For example: Expires:Thu,15 Apr 2019 20:00:00 GMT;This tells the browser cache effectiveness continue until April 15, 2019, using the same cache request within this time, the outside this time using an HTTP request. And the above mentioned Last-Modifiedand If-Modified-Sinceand ETagsand If-None-Matchcompared, are able to save a little bandwidth, because it may be less fat HTTP request.

但是Expires有一个明显的缺点;由于返回的到期时间是服务器端的时间,这样存在一个问题,如果客户端的时间与服务器的时间相差很大,那么误差就很大,所以在HTTP 1.1版开始,使用Cache-Control:max-age=秒替代。如果Cache-ControlExpires同时存在,Cache-Control生效。

由于现在基本上都在使用Cache-Control,所以有必要对Cache-Control进行详细的总结一下。

  • Cache-Control的可缓存性:

    取值 说明
    public HTTP返回的时候在Heaher中设置Cache-Control的值为public。它代表,这个HTTP请求它返回的内容所经过的任何路径中,包括中间的一些HTTP代理服务器以及发出请求的客户端浏览器,都可以进行对返回内容的缓存操作
    private 发起请求的浏览器才能使用返回数据的缓存
    no-cache 可以在本地或者proxy服务器进行缓存,每次发起请求都要去服务器验证,服务器返回可以使用缓存,才可以真正使用本地缓存,任何节点都不能直接使用缓存
  • Cache-Control的有效期

    取值 说明
    max-age=seconds 最常用模式,表示过期的秒数
    s-maxage=seconds 只有在代理服务器才会生效,且代理服务器会优先使用s-maxage
    max-stale=seconds 它是发起请求方,主动去带着的header;在max-age过期后,但还在max-stale的有效期内,还可以使用过期的缓存,不需要去原服务器请求新的内容
  • Cache-Control的其它取值

    取值 说明
    no-store 浏览器或者proxy服务器都不能存返回数据的缓存,永远都需要去服务器请求新的数据
    no-transform 主要用在proxy服务器,表示不要去随意改动返回的内容,比如压缩什么的

总结

这些细小的知识点,平时很少主动去关注,但是真正到分析问题的时候,很多时候却是卡在这些细小的知识点上。还是那句话,细节决定成败!大的知识点,大的流程,大家都可以说出一二,但是一旦细化了,才知道自己有好多的不懂,有好多的说不清楚。只有退潮了,才知道谁在裸泳!!!

2019年7月21日 于内蒙古呼和浩特。

Guess you like

Origin www.cnblogs.com/vipygd/p/11221659.html