Importance of ETag in HTTP parameters

Source: Internet
Author: User

In the study of tornado, there is an etag more curious, from the online query excerpt as follows:

ETag is introduced in HTTP1.1, the main function is to add a (CSS file, image, JavaScript file) a unique parameter (equivalent to the query parameter string), the ETag has server-side generation, and as the file changes, In this way, the browser will only re-request to obtain the ETag changes in the file, reduce browser data flow, speed up the browser response speed, it is important to reduce the pressure on the server side, so the server-side ETag implementation is more important.

Now we have a question. Why use the ETag?

The etag is mainly to solve some problems that last-modified cannot solve. He can know more precisely than last_modified whether a file has been modified or not. If a file is modified very frequently, such as in the time of the second, such as modified in 1 seconds, 10 times, If-modified-since can check only the second level of modification, so this modification cannot be judged. The reason is that UNIX records mtime can only be accurate to seconds. So we choose to generate the ETag, because the etag can synthesize inode,mtime and size to avoid this problem.

How the ETag works
After the etag is generated on the server, the client verifies that the resource is modified by If-match or if-none-match the condition. It is common for us to use If-none-match. The process of requesting a file may be as follows:
A new request
The client initiates an HTTP GET request for a file (CSS, IMAGE,JS), the server processes the request, returns the file contents and a bunch of headers (including the etag, such as "2e681a-6-5d044840"), and the HTTP header status code is 200.

The same user request for this file for the second time
When a client initiates an HTTP GET request for a file, note that at this time the client sends a If-none-match header that includes the last file's ETag (for example, "2e681a-6-5d044840"). The server then determines the etag sent over and its own computed etag, so If-none-match is false, does not return 200, returns 304, and the client continues to use the local cache;

Note that when the server is set up with Cache-control:max-age and expires, it is used at the same time, that is, after the modification time and the ETag are fully matched if-modified-since and If-none-match. The server can return 304.

The following is the configuration of the ETag in Apache

The support for setting the ETag in Apache is relatively simple, just add the following to the Apache configuration:

Fileetag MTime Size

Note: The FILEETAG directive configures the properties of a file that is used to create an ETAG (Entity label) answer header when the document is based on a file (the value of the ETag is for buffer management to conserve network bandwidth). The value of the etag is determined by the Inode (index node), size, and last modified time of the file. The FILEETAG directive allows you to select (if you want to make a selection) which of these elements will be used. The key keywords are as follows:

Inode
Number of index nodes (inode) for the file
MTime
Date and time when the file was last modified
Size
The number of bytes in the file
All
All existing domains, equivalent to: Fileetag INode MTime Size
None
If a document is file-based, no ETag header is included in the answer

In large multi-web clusters, the use of the etag problem, so it is recommended to use the Web cluster does not use the etag, in fact, very good solution, because multiple servers, the inode is not the same, so different server generated etag is not the same, so the user is likely to repeat the download (the ETag will not be allowed to ), understand the above principles and settings, the solution is also very easy, so that the etag after two parameters, mtime and size is good. As long as the ETag calculation does not have inode parameters to calculate, it will be very accurate.

Importance of ETag in HTTP parameters

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.