Etag and Expires Performance Tuning

Source: Internet
Author: User
Tags echo date

1. How the Client-side Http Request Header and Server-side Http Reponse Header work in Etag and Expires.
2. Static configuration of Etag and Expires in Apache, Lighttpd, and Nginx
3. Etag and Expires processing on dynamic pages with non-real-time interaction
When the client sends a Request for a URL for the first time through a browser, the browser will send the Header (HTTP Request Header) to the server according to the Http protocol ), when the server responds with the Http Reponse Header, the server returns 200 in the following format:
HTTP/1.1 200 OK
Date: Tue, 03 Mar 2009 04:58:40 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age = 2592000

Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: "5d8c72a5edda8d6a: 3239 ″

When the client requests this URL for the second time, according to the HTTP protocol, the browser will send an Http Request Header to the server. The server responds and records that the relevant record property Tag file has not been changed, the server returns 304, which is directly read from the cache:
HTTP/1.x304 Not Modified
Date: Tue, 03 Mar 2009 05:03:56 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age = 2592000
Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: "5d8c72a5edda8d6a: 3239 ″
The Last-Modified, Expires, and Etag mark the page cache identifier.

I. Working Principles of Last-Modified, Expires, and Etag
1. Last-Modified
When the browser requests a URL for the first time, the server returns 200, the content is the resource you requested, and there is a Last-Modified attribute mark (Http Reponse Header) the last modification time of this file on the service end. The format is similar to this:
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
When the client requests this URL for the second time, the browser will sendIf-Modified-SinceHeader (Http Request Header) to check whether the file has been modified after this time:
If-Modified-Since: Tue, 24 Feb 2009 08:01:04 GMT
If the resources on the server are not changed, the HTTP 304 (NotChanged.) status code is automatically returned and the content is blank, which saves the amount of data transmitted. When the server code changes or the server is restarted, the resource is re-issued, and the returned result is similar to the first request. This ensures that resources are not repeatedly sent to the client, and that the client can obtain the latest resources when the server changes.
Note: If the time of If-Modified-Since is later than the current time of the server (the current request time request_time), it is regarded as an invalid request.

2. Working Principles of Etag
The HTTP specification defines ETag as "OBJECT tag of the requested variable" (see 14.19 ). Simply mark the request URL when the server responds and send it to the client in the HTTP Response Header, similar to the format returned by the server:
Etag: "5d8c72a5edda8d6a: 3239 ″
The query update format of the client is as follows:
If-None-Match: "5d8c72a5edda8d6a: 3239 ″
If ETag does not change, status 304 is returned.
That is, after the client sends a request, the Http Reponse Header contains Etag: "5d8c72a5edda8d6a: 3239 ″
ID, indicating that the Client has the ID: 5d8c72a5edda8d6a: 3239. When the next Request is sent to Request the same URI, the browser sends an If-None-Match header (Http RequestHeader) at the same time. The header contains the Etag: "5d8c72a5edda8d6a: 3239 mark.
If-None-Match: "5d8c72a5edda8d6a: 3239"
In this way, the Client is equal to two copies of the Cache, and the server compares the etag of the two. If-None-Match is False and 200 is Not returned, 304 (Not Modified) Response is returned.

3. Expires
After the given date/time, the response is considered obsolete. For example, Expires: Thu, 02 Apr 2009 05:14:08 GMT
AndLast-ModifiedIn combination. It is used to control the validity period of the request file. When the request data is within the validity period, the client browser requests the data from the cache instead of the server. When the data in the cache expires or expires, it is decided to update the data from the server.

4. Last-Modified and Expires
The Last-Modified mark can save a little bandwidth, but it still cannot escape sending an HTTP request and must be used with Expires. The Expires mark does not allow the browser to send HTTP requests. For example, when the user F5 or click the Refresh button, an HTTP request is sent even if the user has Expires Uris, therefore, Last-Modified must be used together with Expires.

5. Etag and Expires
If both Etag and Expires are set on the server, the Etag principle is the same, that is, the HttpRequest Header: If-Modified-Since and If-None-Match corresponding to Last-Modified/Etag. We can see that the values of these two headers are exactly the same as those of Last-Modified and Etag sent by WebServer; if-Modified-Since and If-None-Match are completely matched, that is, after checking the modification time and Etag, the server can return 304.

6. Last-Modified and Etag
The http header of the Last-Modified and ETags requests is used together. The server first generates the Last-Modified/Etag mark. The server can use it later to determine whether the page has been Modified, to determine whether the file is cached.
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.
Note:
1. The Last-Modified and Etag headers are both Http Reponse headers sent by the Web Server. The Web Server should support both headers.
2. After the Web Server sends the Last-Modified/Etag headers to the client, the client caches these headers;
3. When the client initiates a request for the same page again, it will send the Http RequestHeader: If-Modified-Since and If-None-Match corresponding to Last-Modified/Etag respectively. We can see that the values of these two headers are exactly the same as those of Last-Modified and Etag sent by WebServer;
4. Check with the server to determine whether the file continues to be cached;

Ii. Configure Etag and Expires in Apache, Lighttpd, and Nginx to effectively cache files such as css, js, pic, page, and streaming media.
A. Expires
A.1, Apache Etag
Use the mod_expires module of Apache to set the parameters. This includes the Expires header content for Control of the response and the max-age command for the Cache-Control header.
ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/html "access plus 30 minutes"
ExpiresByType text/css "access plus 30 minutes"
ExpiresByType text/txt "access plus 30 minutes"
ExpiresByType text/js "access plus 30 minutes"
ExpiresByType application/x-javascript "access plus 30 minutes"
ExpiresByType application/x-shockwave-flash "access plus 30 minutes"
Or
<Ifmodule mod_expires.c>
<Filesmatch "\. (jpg | gif | png | css | js) $">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</Filesmatch>
</Ifmodule>
When expires is set, the max-age information of Cache-Control is automatically output.
For more information about Expires, see Apache official documentation.
During this period, all requests to this file will be obtained directly from the cache server,
Of course, if you need to ignore the browser's refresh request (F5), the Cache Server squid also needs to use the refresh_pattern option to ignore this request.
Refresh_pattern-I \. gif $1440 100% 28800 ignore-reload
Refresh_pattern-I \. jpg $1440 100% 28800 ignore-reload
Refresh_pattern-I \. jpeg $1440 100% 28800 ignore-reload
Refresh_pattern-I \. png $1440 100% 28800 ignore-reload
Refresh_pattern-I \. bmp $1440 100% 28800 ignore-reload
Refresh_pattern-I \. htm $60 100% ignore-reload
Refresh_pattern-I \. html $1440 50% 28800 ignore-reload
Refresh_pattern-I \. xml $1440 50% 28800 ignore-reload
Refresh_pattern-I \. txt $1440 50% 28800 ignore-reload
Refresh_pattern-I \. css $1440 50% 28800 reload-into-ims
Refresh_pattern-I \. js $60 50% 100 reload-into-ims
Refresh_pattern. 10 50% 60
For more information about Expires in Squid, see refresh_pattern in Squid.

A.2. Lighttpd Expires
Like Apache, when setting expire in Lighttpd, you must first check whether the mod_expire module is supported,
The following settings expire after 1 hour for all the files in the images directory in the URI;
Expire. url = ("/images/" => "access 1 hours ")
The following is a file that acts on the images directory and Its subdirectories;
$ HTTP ["url"] = ~ "^/Images /"{
Expire. url = ("" => "access 1 hours ")
}
You can also specify the file type;
$ HTTP ["url"] = ~ "\. (Jpg | gif | png | css | js) $ "{
Expire. url = ("" => "access 1 hours ")
}
For details, refer to the official Lighttpd Expires explanation.

A.3. Expires in Nginx
Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
{
Expires 30d;
}
Location ~ . * \. (Js | css )? $
{
Expires 1 h;
}
This type of file is not often modified. The expires command is used to control its cache in the browser to reduce unnecessary requests. The expires command can Control the header labels of "Expires" and "Cache-Control" in the HTTP Response (to Control the page Cache ). For more information, see Expires in Nginx.

B .1. Etag settings in Apache
Setting Etag in Apache is relatively simple. You only need to create a file. htaccess in the directory containing static files, which includes:
FileETag MTime Size
In this way, you can refer to the FileEtag document page of Apache for details.

B .2. Lighttpd Etag
Set Etag support in Lighttpd:
Etag. use-inode: whether to use inode as Etag
Etag. use-mtime: whether to use the file modification time as Etag
Etag. use-size: whether to use the file size as Etag
Static-file.etags: whether the Etag feature is enabled
The fourth parameter must be enable. Select the first three parameters based on actual needs. The modification time is recommended.

B .3. Nginx Etag
By default, Nginx does not add the idea of Etag identification. Igor Sysoev. "It cannot be seen how Etag is better than Last-Modified identification in static file processing. "
Note:
Yes, it's addition, and it's easy to add, however, I do not see howETag is better than Last-Modified for static files.-Igor Sysoev
A nice short description is here:
Http://www.mnot.net/cache_docs/#WORK
It looks to me that it makes some caches out there to cache theresponse from the origin server more reliable as in rfc2616 (ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt) is written.
3.11 Entity Tags 13.3.2 Entity Tag Cache Validators 14.19 ETag
Of course, there are third-party nginx-static-etags modules. Please refer
Http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

Iii. Processing of Epires and Etag on dynamic pages with non-real-time interaction
You can consider caching data that is not updated frequently, such as tag classification and archiving. Simply put, the expires and etag identifiers are output in dynamic programs with non-real-time interaction, so that they can be cached. However, you must disable the session to prevent the http header from containing the session id during http response;
3.1. Expires
For example, expires. php
<? Php
Header ('cache-Control: max-age = 86400, must-revalidate ');
Header ('Last-Modified: '. gmdate ('d, d m y h: I: s'). 'gmt ');
Header ("Expires:". gmdate ('d, d m y h: I: s', time () + '000000'). 'gmt ');
?>
The above information indicates that the file expires 24 hours after the request.
Other dynamic pages to be processed can be called directly.
3.2. Etag
Processing is based on the Http return status. When 304 is returned, it is directly read from the cache.
For example, etag. php
<? Php
Cache ();
Echo date ("Y-m-d H: I: s ");
Function cache ()
{
$ Etag = "http://www.jb51.net ";
If ($ _ SERVER ['HTTP _ IF_NONE_MATCH '] = $ etag)
{
Header ('etag: '. $ Etag, true, 304 );
Exit;
}
Else header ('etag: '. $ Etag );
}
?>

We know that reasonable etag configuration can save a lot of bandwidth.
In fact, configuring the etag cache does not enable the Last-Modified header.
When we do not know the modification time, Etag is useful for dynamic content.

Cell phone home Gao chunhui about etag supplement:
There are several differences or areas to be supplemented:
1. "Of course, Etag does not have a significant impact on the performance of most sites ." It should be said that when Etag is correctly used, it will make a large number of requests respond in the form of 304 headers, which can greatly save server resources and bandwidth. In the past, Etag was not used in some cases. The Etag calculation method based on some webservers contains inode, which is unavailable for multiple web servers, you can change the calculation method.
2. PHP is almost impossible to refresh this point as soon as possible. Even if you execute flush and similar functions, it will not be output to the browser end until the request is fully executed.
3. AJAX has advantages in using GET and POST. The GET method can respond faster, but it may be cached by the browser. Generally, a random number is required to avoid this problem. The POST method does not. Therefore, it is best to use the GET and POST methods based on your own situation.

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.