Introduction to HTTP cache headers Last Modified and ETag

  • 2020-05-15 03:05:06
  • OfStack

The first request

Request:


GET /pic/201408/102.jpg HTTP/1.1
Host: www.ofstack.com
Connection: keep-alive
Cache-Control: no-cache
Accept: image/webp,*/*;q=0.8
Pragma: no-cache
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Referer: //www.ofstack.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

Response:


HTTP/1.1 200 OK
Content-Length: 66529
Content-Type: image/jpeg
Last-Modified: Tue, 19 Aug 2014 12:23:54 GMT
Accept-Ranges: bytes
ETag: "029e570a8bbcf1:1ae2"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 20 Aug 2014 00:29:03 GMT xxx

Second request

Request:


GET /pic/201408/102.jpg HTTP/1.1
Host: www.ofstack.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: image/webp,*/*;q=0.8
Pragma: no-cache
If-Modified-Since: Tue, 19 Aug 2014 12:23:54 GMT
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
If-None-Match: "029e570a8bbcf1:1ae2"
Referer: //www.ofstack.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

Response:


HTTP/1.1 304 Not Modified
Last-Modified: Tue, 19 Aug 2014 12:23:54 GMT
Accept-Ranges: bytes
ETag: "029e570a8bbcf1:1ae2"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 20 Aug 2014 00:29:54 GMT

The HTTP cache is used to save the amount of data transferred over the network. When the resources on the server side do not change, it returns 304 and the contents are empty. Both Last-Modified and ETag are used for the HTTP cache, which the browser passes back to the server on the second request to determine if the resource has changed. ETag was introduced as a supplement to Last-Modified by the HTTP/1.1 standard for the following reasons:

· 1. For files that have been modified periodically, the modification time has changed but the content has not changed, so I don't want to re-GET at this time;
· 1. Some files are modified very frequently, for example, several times in one second, while Last-Modified can only be accurate to the second;
· 1 some servers cannot get the exact time of file modification;

Note: the HTTP/1.1 standard does not specify what Etag is or how it should be implemented, only 1 specifies that Etag should be enclosed in quotation marks.


Related articles: