Expires,cache-control,last-modified,etag

Source: Internet
Author: User
Tags response code time limit
Expires, Cache-control, last-modified, ETag are all in the return header of the HTTP response
Used to control the browser client cache behavior.
1.Expires is defined in http/1.0, cache-control:max-age=xxx is defined in http/1.1,
For backwards compatibility, only using Max-age is not enough.
When a Web resource is accessed, it is cached by the browser and will be accessed again later by the same resource.
Check that the cache is out of date.
expires specifies that the value is a date, and that the cache is considered valid until the specified date arrives before it is accessed again.
MAX-AGE specifies the number of seconds that the cache will be considered valid if it is accessed again after a number of seconds from the first access.
There is no network request at this time.
Scene: 1, the user in the browser entered a once visited URL, 2, click the URL link (href) link to a once visited
Resources. 3, the static js,css in the webpage, the picture resource.

The value of last-modified is a date that is the time the resource was last modified.
The value of the ETag is a value computed by the hash algorithm for the contents of the server on the requested resource, when the content
When a change occurs, the value changes accordingly.

Expires and Cache-control control whether the browser does cache, and if so, how long the cache is valid. Expires values of 1 and 0 are not cached. The value of Cache-control is No-cache not cached.
The last-modified and ETag are the comparison values when the cache expires to the server for validation. Last-modified can only be accurate to the second level, and if you want to go to the millisecond level, use the ETag.

When the cache expires, the browser sends a request to the server.
The request header will take the if-modified-since and If-none-match parameters.
The value of If-modified-since is the value of the last-modified that was originally received in the cache.
The value of If-none-match is the value of the ETag that was originally received in the cache.
If there is no last-modified and ETag value in the initial return, this request parameter is not taken.
The server "verifies" when it is received, and returns a "304 not Modified" response if the same
The content is not transmitted again, saving bandwidth.
Scenario: 1, the access time after the expires date or max-age the specified time, 2, the user press F5 refresh.

There is also a case where the user presses CTRL+F5, and the browser will be in the request header with
The pragma parameter, with a value of No-cache, removes If-modified-since and if-none-match from the request header.
When the server receives the request, it re-passes the content to the browser, regardless of whether the content changes, as if it were the first time the browser requested it.
The response code is OK.

Here's how to set these parameters in Java code:
The browser side or cache server is not allowed to cache the current page information.
Response.setdateheader ("Expires", 0);
Response.AddHeader ("Cache-control", "No-cache");
This page allows caching on the browser side or cache server with a time limit of 10 seconds.
Java.util.Date Date = new Java.util.Date ();
Response.setdateheader ("Expires", Date.gettime () +10000);
Response.AddHeader ("Cache-control", "max-age=10");
Setting the modification time
Response.setdateheader ("Last-modified", Date.gettime ());

In Nginx experiment, Nginx can be configured by expires or Add_header to
Response header adds expires and Cache-control parameters.
When not specified by default, the response header is as follows, with no expires and Cache-control parameters.
By default there are last-modified and ETag values, and multiple accesses do not occur for network requests.
Connection keep-alive
Date Wed, 05:17:24 GMT
Etag "52957fcb-37"
Last-modified Wed, 05:14:51 GMT
Server nginx/1.5.7
NGINX Specifies expires-1d, meaning that the expiration time is one day before.
At this point each time another access to the same resource will go to the server "verify" and then get a 304 return.
Cache-control No-cache
Connection keep-alive
Date Wed, 06:52:20 GMT
Etag "5295898b-b6"
Expires Tue, 06:52:20 GMT
Last-modified Wed, 05:56:27 GMT
Server nginx/1.5.7
NGINX Specifies expires 1d, meaning that the expiration time is one day later.
Each time the same resource is accessed again, the browser does not send a request like a server.
The browser will not know if the contents of the resource are changed.

Of course if you F5 or ctrl+f5 you can see the latest content.

Other values that the Cache-control can also set:


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.