Browser Cache detailed Description: Expires,cache-control,last-modified,etag

Source: Internet
Author: User
Tags browser cache

Recently in the optimization of CDN, browser cache In-depth research, a note, to facilitate the post-comers

Drew a Sketch:



Detailed descriptions of each status are as follows:

1 , last-modified

When the browser first requests a URL, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute tag (httpreponse Header), which is the last time the file was modified at the service end, in a format similar to this:

Last-modified:tue, Feb 2009 08:01:04 GMT

The second time the client requests this URL, according to the HTTP protocol, the browser sends the If-modified-since header (HttpRequest header) to the server, asking if the file has been modified after that time:

If-modified-since:tue, Feb 2009 08:01:04 GMT

If the server-side resources do not change, the HTTP304 (notchanged.) Status code is automatically returned, and the contents are empty, thus saving the amount of data transferred. When the server-side code changes or restarts the server, the resource is issued again, similar to the first request. This ensures that resources are not duplicated to the client and that the client is able to get the latest resources when the server changes.

Note: If the if-modified-since time is later than the current time of the server (current request time request_time), it will be considered an illegal request.


2 , Etag Working principle

The HTTP protocol specification defines etag as the entity tag of the requested variable (see 14.19). A simple point is that the request URL is marked when the server responds and is routed to the client in the HTTP response header, similar to the format returned by the server side:

Etag: "5d8c72a5edda8d6a:3239″

The client's query update format is like this:

If-none-match: "5d8c72a5edda8d6a:3239″

If the etag does not change, the status 304 is returned.

That is: After the client makes a request, the Httpreponse header contains ETag: "5d8c72a5edda8d6a:3239″

The identity is equal to telling the client side that you get the resources for this to have an indication of id:5d8c72a5edda8d6a:3239. The next time the request asks for the same URI, the browser emits a If-none-match header (Http requestheader) at the same time, the information in the header contains the last access ETag: "5d8c72a5edda8d6a : 3239″ identification.

If-none-match: "5d8c72a5edda8d6a:3239"

, so that the client end equals cache two, the server side will be compared to 2 of the etag. If If-none-match is false and does not return 200, 304 (not Modified) Response is returned.


3 , Expires

Given the date/time, the response is considered obsolete. such as Expires:thu, APR 2009 05:14:08 GMT

Need to be used in conjunction with last-modified. Used to control the time that the request file is valid, and when the request data is within the validity period, the client browser requests data from the cache instead of the server side. When the data in the cache is invalidated or expired, the data is not determined to be updated from the server.


4 , last-modified and Expires

The Last-modified logo can save a bit of bandwidth, but it's still not going to run out of an HTTP request and be used with expires. The Expires logo makes the browser simply not to send HTTP requests, such as when the user F5 or click the Refresh button, even if there is a expires URI, also send an HTTP request out, so, last-modified still need to use, and to use it with expires.



5 , Etag and Expires

If the server side sets both ETag and expires, the etag principle is the same as the httprequestheader:if-modified-since and if-none-match corresponding to the Last-modified/etag. We can see that the values of these two headers are exactly the same as the last-modified,etag values emitted by webserver, and after the exact match if-modified-since and If-none-match are checked for modification time and ETag, The server can return 304.



6 , last-modified and Etag

The last-modified of files between multiple machines in a distributed system must be consistent to avoid load balancing to different machines causing failure

Distributed systems try to shut down ETag (each machine generates etag will be different)

Used with the HTTP header of the last-modified and ETAGS requests, the server first produces the Last-modified/etag tag that the server can use later to determine whether the page has been modified to determine whether the file continues to cache

The process is as follows:

1. Client requests a page (a).

2. The server returns page A and adds a last-modified/etag to a.

3. The client presents the page and caches the page along with the Last-modified/etag.

4. The Customer requests page A again and passes the Last-modified/etag returned by the server to the server together with the last request.

5. The server checks the last-modified or ETag and determines that the page has not been modified since the last client request, returning directly to response 304 and an empty response body.

Note:

1, last-modified and ETag heads are issued by the Webserver Httpreponse Header,webserver should support both types of head.

2, Webserver sent the Last-modified/etag head to the client, the client will cache these headers;

3, the client launches the same page request again, will send separately with last-modified/etag corresponding httprequestheader:if-modified-since and If-none-match. We can see that the values of these two headers are exactly the same as the last-modified,etag values emitted by webserver;

4, through the above value to the server side check to determine whether the file continues to cache;

7. About cache-control:max-age= sec and Expires

Expires = time, HTTP 1.0 version, cache load time, allow the client not to check before this time (send request)
Max-age = seconds, HTTP 1.1 version, how many seconds the resource is cached locally.
If Max-age and expires exist at the same time, they are overwritten by Cache-control Max-age.

A disadvantage of Expires is that the return expiration time is the server-side time, there is a problem, if the time of the client and the server time difference is very large, so the error is very large, so in the HTTP 1.1 version, the use of cache-control:max-age= second substitution.

Expires =max-age + "current request time per download"
So once the page is downloaded again, expires is recalculated once, but last-modified does not change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.