"Reprint" Browser cache detailed: Expires Cache-control last-modified

Source: Internet
Author: User
Tags browser cache

Recently in the optimization of the CDN, the browser cache in-depth study, a record, convenient after the comer

Draw a Sketch:

A detailed description of each status is as follows:

1 , last-modified

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

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

When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the if-modified-since header (HttpRequest header) to the server, asking if the file has been modified since:

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

If the server-side resource does not change, the HTTP304 (notchanged.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the 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 (the current request time Request_time), it will be considered an illegal request .

2 , how the Etag works

The HTTP protocol specification defines the ETag as the entity tag of the requested variable (see 14.19). The simple point is that the server responds with the request URL token and transmits it 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 this:

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

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

That is : After the client makes the request, theHttpreponse header contains the Etag: "5d8c72a5edda8d6a:3239″

Identity, which is equal to telling the client side, you get this resource has the expression ID:5d8c72a5edda8d6a:3239. The next time you request a request for the same URI, the browser issues a If-none-match header (Http Requestheader) that contains the Etag that was last accessed in the header : " 5d8c72a5edda8d6a:3239″ identification.

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

, so that theclient side equals the cache two copies, the server side will be compared to 2 of the ETag. If If-none-match is False, returns 200, returning 304 (not Modified) Response.

3 , Expires

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

Must be used in conjunction with the last-modified. Used to control the validity time of the request file, when the request data is within the validity period, the client browser requests data from the cache instead of the server side . When data in the cache fails or expires, the data is updated from the server.

4 , last-modified and Expires

The Last-modified logo can save a bit of bandwidth, but still can't get out of an HTTP request and use it with expires. and the Expires logo makes the browser even if the HTTP request does not have to send, for example, when the user F5 or click the refresh button even for the expires URI, the same will send a HTTP request out, so,last-modified still need to use, and to use with expires.



5 , Etag and Expires

If the etag and expires are set simultaneously on the server side, the etag principle is thesame, i.e. the httprequestheader:i corresponding to the Last-modified/etag F-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 the webserver; in the exact match If-modified-since and If-none-match that the server can return 304 after the modification time and ETag are checked.



6 , last-modified and Etag

The last-modified of multiple machine files in a distributed system must be consistent to avoid load balancing to different machines resulting in comparison failures

The distributed system shuts down the etag as much as possible (the etag generated by each machine will be different)

Last-modified is used with the HTTP header of the etags request , the server first generates the Last-modified/etag tag, which the server can later use to determine whether the page has been modified to determine whether the file continues to be cached

The process is as follows :

1. The 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 Last-modified/etag.

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

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

Note:

1,last-modified and etag headers are issued by the webserver httpreponse header,webserver should support both of these headers.

2,webserver after sending the Last-modified/etag header to the client, the client will cache these headers;

3. When the client initiates a request for the same page again, the httprequestheader:if-modified-since and if-none-match corresponding to the Last-modified/etag will be sent separately . We can see that the values of the two headers are exactly the same as the Last-modified,etag values emitted by the 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= seconds and Expires

Expires = time, HTTP 1.0 version, cache load time, allow client to not 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 covered by Cache-control max-age.

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

Expires =max-age + "Current request time for each download"

So once the page has been re-downloaded, the expires is recalculated, but last-modified does not change

"Reprint" Browser cache detailed: Expires Cache-control last-modified

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.