Etag of HTTP Caching

Source: Internet
Author: User

Etag

Edit entry Sharing

This entry was created by LCLNL, and has been edited 2 times by a total of 1 collaborators. The newest association author: lclnl. Please describe the term in a simple sentence and add a summary immediately. directory 1 how last-modified and etags help improve performance? 2 Function 3 Working principle 4 Apache ETag to achieve 5 weak checksum (weak etag) for this entry add video and photos related images

The HTTP protocol specification defines etag as the entity value of the requested variable.

Alternatively, ETag is a token (token) that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for determining what the token is and what it means, and sending it to the client in the HTTP response header, the following is the format returned by the server side:

ETag: "50b1c1d4f775c61:df3"

The client's query update format is like this:

If-none-match:w/"50b1c1d4f775c61:df3"

If ETag does not change, it returns state 304 and does not return, as is the case with last-modified.

Testing ETag is primarily useful when a breakpoint is downloaded. how do etag-last-modified and etags help improve performance?

Smart developers use the HTTP headers of last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates a Last-modified/etag tag, the server can use it later to determine if the page has been modified. Essentially, the client requires the server to authenticate its (client) cache by passing the token back to the server.
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.
Etag-function

Etag mainly to solve some problems that last-modified can't solve.

1, some files may be periodically changed, but his content does not change (just change the modified time), this time we do not want the client to think that the file has been modified, and get back;

2, some file modification is very frequent, such as in the seconds below the time to modify, (such as the 1s modified n times), if-modified-since can check the granularity is S-class, this modification can not be judged (or UNIX records mtime can only be accurate to the second)

3, some servers can not accurately get the final modification of the file time;

to this end, http/1.1 introduced Etag (Entity Tags). ETag is just a file-related tag, and can be a version tag, such as v1.0.0 or "2e681a-6-5d044840", a string of seemingly cryptic encodings. But the http/1.1 standard does not stipulate what the content of ETag is or how to achieve it, the only stipulation is etag need to put in "". Etag-Working principle

ETag is generated by the server side, and the client verifies the resource modification by If-match or if-none-match this condition to determine the request. It is common to use if-none-match. The process of requesting a file may be as follows:

= = = First request = = =
1. The client initiates HTTP get request a file;
2. The server processes the request, returns the contents of the file and a heap header, including, of course, etag (for example, "2e681a-6-5d044840") (assuming that the server supports ETag generation and has opened ETag). Status Code 200

= = = Second Request = = =
1. The client initiates HTTP GET request a file, notice this time the client sends a If-none-match header simultaneously, this header's content is the first time request the server returns etag:2e681a-6-5d044840
2. The server judge sent over the ETag and calculated ETag match, so If-none-match is false, does not return 200, returns 304, the client continues to use the local cache;

The process is very simple, the question is, if the server has set up cache-control:max-age and expires, how to do.
The answer is simultaneous use, which means the server can return 304 after the exact match if-modified-since and If-none-match are checked for modification time and ETag. (Don't get caught up in the problem of who to use.)
Etag implementation in Etag-apache

1.Apache first judge is not weak etag, this stays here to say. If not, enter the second scenario:

Strong ETag sets the ETag value according to the configuration in the configuration file, and the default Apache Fileetag is set to:

Fileetag INode mtime Size

That is, based on these three properties to generate the ETag value, they are implemented by some algorithms, and output into the hex format, adjacent properties between the-separated, such as:

Etag "2e681a-6-5d044840"

The three paragraphs in this section represent the hex format of the value inode,mtime,size based on the algorithm (if you see a character in a non-hex (i.e. 0-f), you may see God:)

Of course, you can change the Apache Fileetag settings, such as set to Fileetagsize, then the resulting etag may be:

Etag "6"

In short, set a few paragraphs, the ETag value has several segments. (Don't assume that ETag is a fixed 3-paragraph)

Description
Here is the Apache2.2 inside the ETag implementation, because http/1.1 does not specify what ETag must be the implementation or format, therefore, can also modify or completely write their own algorithm to get etag, such as "2e681a65d044840", The client remembers and caches the ETag (where Windows is saved, and the next time you visit it, take this value to the server-generated etag contrast.

Attention
No matter how the algorithm, on the server side to be calculated, the calculation has cost, will bring performance loss. So in order to squeeze this little bit of performance, many sites completely disable ETag (such as Yahoo!), which actually does not conform to the http/1.1 rules, because http/1.1 always encourage the server to open as much as possible etag.
Etag-Weak check (weak Etag)

Reconsider the 3 issues mentioned earlier:

Problem 1, some files may be periodically changed, but his content does not change (just change the time of modification), this time we do not want the client to think that the file has been modified, and get back;

Solution: If the use of strong Etag, each time you will require a Get page, if the use of Etag, for example, set to File Etag Size, you can ignore mtime caused by last-modified time changes to affect the If-modified-since (IMS) This checksum. This has nothing to do with weak etag.

Problem 2, some files are modified very frequently, such as in the time under the second change, (such as the 1s modified n times), if-modified-since can check the granularity is S-class, this modification can not be judged (or UNIX records mtime can only be accurate to the second)

Solution: If this is the case, Apache will automatically determine the difference between the request time and the modified time, if less than 1s,apache will think that the file in the 1 seconds may be modified again , thus generating a weak etag (Weaketag), This etag is only based on mtime, so mtime can only be accurate to s, so the etag generated within 1s is always the same, thus avoiding the use of strong etag caused by frequent flush cache in 1s. (seemingly not etag, only use last-modified can be solved, but this is only to modify the super frequent situation, many files may also use strong etag authentication). Weak etag start with w/, such as: w/"2e681a"

Problem 3, some servers can not accurately get the last modification time of the file;

Workaround: Generate ETag, because ETag can synthesize inode,mtime and size to avoid this problem

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.