Re: http://www.oschina.net/question/234345_42536? Sort = Time
If the server is a cluster, the etag parameters in the HTTP header returned by different servers are different. If the image is generated by a program, we can use the no-Cache headers to control it. But what if these images are Apache or nginx?
The following describes etag:
Etag is introduced in http1.1. The main function is to add a unique parameter (equivalent to a query parameter string) after the (CSS file, image, Javascript file) file. etag is generated on the server side, as the file changes, the browser will only request to obtain the file with the etag changed, reduce the data traffic on the browser side, and speed up the response of the browser, it is important to reduce the pressure on the server, so the implementation of the etag on the server is more important.
Now we have a question: why should we use etag?
Etag is mainly used to solve some problems that last-modified cannot solve. it is more accurate to know whether the file has been modified than last_modified. if a file is frequently modified, for example, modified within seconds, for example, 10 times in 1 second, if-modified-since can check that only modifications can be made in seconds, therefore, this modification cannot be judged. the reason is that the mtime of UNIX records can only be accurate to seconds. therefore, we chose to generate etag because etag can combine inode, mtime, and size to avoid this problem.
How etag works
After the etag is generated on the server, the client determines whether to modify the resource through the condition of IF-match or if-None-match. We usually use if-None-match. To request a file, the process may be as follows:
New request
The client initiates an http get request to a file (CSS, image, JS). The server processes the request and returns the file content and a bunch of headers (including etag, such as "2e681a-6-5d044840 "), the HTTP header status code is 200.
The second request for this file from the same user
The client initiates an http get request to a file at a time. Note that the client sends an IF-None-match header at the same time, this header will include the etag of the last file (for example, "2e681a-6-5d044840"). Then the server judges the etag sent and the etag calculated by itself, therefore, if-None-match is false, 200 is not returned, and 304 is returned. The client continues to use the local cache;
Note. when cache-control: Max-age and expires are set on the server, 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.
The following is the configuration of etag in Apache.
Setting etag in Apache is simple. You only need to add the following content to Apache configuration:
Fileetag mtime size
Note: The fileetag command configures the attributes of the file used to create the etag (entity tag) response header when the document is based on a file (the value of etag is used for buffer management to save network bandwidth ). The etag value is determined by the inode (index node), size, and last modification time of the file. The fileetag command allows you to select (if you want to select) which elements will be used. The main keywords are as follows:
Inode
Number of file index nodes (inode)
Mtime
Last file modification date and time
Size
Number of bytes of the file
All
All existing fields, equivalent to: fileetag inode mtime size
None
If a document is file-based, it does not contain any etag header in the response.
When using etag in large and multi-web clustersTherefore, it is recommended that you do not use etag when using a Web Cluster. In fact, it is a good solution, because the inode is different when there are multiple servers, so the etag generated by different servers is different, therefore, the user may download the file repeatedly (at this time, the etag will not be allowed). After understanding the above principles and settings, it is easy to solve the problem. Let the etag be followed by two parameters, mtime and size are enough. as long as the etag calculation does not involve inode in the calculation, it will be very accurate.
It is also very easy for a dynamic program to generate etag, such as: $ etag = '"'. MD5 ($ body ).'"';