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) This file was last modified at the end of the service period, in the same format:
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. ETag working principle
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, the Httpreponse header contains the ETag: "5d8c72a5edda8d6a:3239″
The identity, which is equal to telling the client side, you get this resource has the expression id:5d8c72a5edda8d6a:3239. The next time a request is requested for the same URI, the browser issues a If-none-match header (Http Requestheader) at which the information in the header contains the etag that was last accessed: "5d8c72a5edda8d6a : 3239″ identification.
If-none-match: "5d8c72a5edda8d6a:3239"
, so that the client 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 simply even HTTP requests do not have to send, such as when the user F5 or click the Refresh button even for the expires URI, the same will send an HTTP request out, so, last-modified still need to use, and to use it with expires.
5. ETag and expires
If both the ETag and the expires are set on the server side, 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 value of these two headers is exactly the same as the Last-modified,etag value emitted by the webserver, and after the exact match if-modified-since and If-none-match are examined, the modification time and the ETag are The server can return 304.
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
A brief analysis of some variables in 86-browser cache