Etag and HTTP Cache Mechanism

Source: Internet
Author: User

Etag-last-modifiedAnd how does etags help improve performance?

Use the HTTP header of the last-modified and etags requests together, so that the cache of the client (such as a browser) can be used. Because the server first generates the last-modified/etag mark, the server can use it later to determine whether the page has been modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.
The process is as follows:
1. The client requests a page ().


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


3. The client displays the page and caches the page together with last-modified/etag.


4. The customer requests page a again and passes the last-modified/etag returned by the server in 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. The server returns the response 304 and an empty response body.

 


Etag-Function

Etag is mainly used to solve some problems that last-modified cannot solve.

1, Some files may be changed cyclically, but their content does not change (only change the modification time)At this time, we do not want the client to think that the file has been modified, but to get it again;


2, Some files are frequently modified, for example, modification within seconds or less(For example, if-modified-since can check that the granularity is s-level. This modification cannot be judged (or the Unix record mtime can only be accurate to seconds)

 

3, Some servers cannot accurately obtain the last modification time of the file;

Therefore, etag (entity tags) is introduced in HTTP/1.1 ). etag is only a file-related identifier. It can be a version identifier, such as v1.0.0 or "2e681a-6-5d044840. However, the HTTP/1.1 Standard does not specify what the etag content is or how to implement it. The only rule is that the etag needs to be placed in.

 


Etag-Working Principle

The etag is generated by the server. The client checks whether the request modifies the resource through the condition of IF-match or if-None-match. It is common to use if-None-match. The process for requesting a file may be as follows:

====First Request =
1. The client initiates an http get request for a file;


2. The server processes the request, returns the file content and a bunch of headers, including etag (for example, "2e681a-6-5d044840") (assuming that the server supports etag generation and etag has been enabled). Status Code 200

====Second request ====
1. The client initiates an http get request for a file. Note that the client sends an IF-None-match header at the same time. The content of this header is the etag: 2e681a-6-5d044840 returned by the server during the first request.


2. The server determines that the sent etag matches the calculated etag. Therefore, if-None-match is false, 200 is not returned, and 304 is returned. The client continues to use the local cache;

The process is very simple. The problem is, what if the server has set cache-control: Max-age and expires?
The answer is to use it at the same time. That is to say, if-modified-since and if-None-match are completely matched. That is, after checking the modification time and etag, the server can return 304.

 

Extension: cache mechanism in HTTP

The cache of web pages is controlled by "cache-control" in the HTTP message header. common values include private, no-cache, Max-age, and must-revalidate, the default value is private. The function of browser review is divided into the following situations:

(1) open a new window
If the values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened.
If the max-age value is specified, the server will not be accessed again within the time range. For example:
Cache-control: Max-age = 5 (indicating the number
Second access will not go to the server)

(2) Press enter in the address bar.
If the value is private or must-revalidate, the server will be accessed only during the first access and will not be accessed in the future.
The value is no-cache.
If the value is Max-age, the access will not be repeated before expiration.

(3) Back and press the button
If the values are private, must-revalidate, and Max-age, no re-access is performed,
If the value is no-cache, access is repeated each time.

(4) press the refresh button
No matter what the value is, it will be accessed again

When the cache-control value is "no-Cache", accessing this page does not leave page backup in the temporary article folder on the Internet.

 

HeaderCommon commands
The header is divided into three parts:
The first part is the HTTP Version );
The second part is the status code );
The third part is the reason phrase (reason-phrase ).

// Fix 404 pages: use this header command to solve the 404 header generated by URL rewriting.
Header ('HTTP/1.1 200 OK ');

// Set 404 header: Page not found
Header ('HTTP/1.1 404 Not found ');

// The page is permanently deleted. You can tell the search engine to update their URLs.
// Set moved permanently header (good
Redrictions)
// Use with location header
Header ('HTTP/1.1 301 moved
Permanently ');

// Restricted access
Header ('HTTP/1.1 403 Forbidden ');

// Server error
Header ('HTTP/1.1 500 internal server error ');

 

Disable page caching in IE 

HTTP Response Message Header settings:

Cachecontrol = No-Cache
Pragma = No-Cache
Expires =-1

 

Expires indicates that there is a time, allowing the client not to check (send a request) before this time, equivalent to the max-age
Effect. If both exist, it will be overwritten by the Max-age of cache-control.

 

HTTP Response Optimization

1. Remove date and expires

2. Only cache-control is retained to control the local cache.

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.