ETag and Expires Performance Tuning _ server Other

Source: Internet
Author: User
Tags current time echo date http request session id
1, ETag and expires the client-side HTTP Request header and server-side HTTP reponse Header working principle.
2, static under the Apache, Lighttpd and Nginx ETag and expires configuration
3. ETag and expires processing in non-real-time interactive dynamic pages
When the client makes the first request for a URL through the browser, according to the HTTP protocol, the browser sends the server header (the HTTP Request header), and the server-side response records the associated attribute tag (the HTTP reponse header). The server-side return status will be 200, in a format similar to the following:
http/1.1 OK
Date:tue, Mar 2009 04:58:40 GMT
Content-type:image/jpeg
content-length:83185
Last-modified:tue, Feb 2009 08:01:04 GMT
cache-control:max-age=2592000

Expires:thu, APR 2009 05:14:08 GMT
Etag: "5d8c72a5edda8d6a:3239″

The second time the client requests this URL, according to the HTTP protocol, the browser will send the server header (HTTP Request header), server-side response and record the related record property tag file has not changed, the server side returned 304, read directly from the cache:
http/1.x 304 Not Modified
Date:tue, Mar 2009 05:03:56 GMT
Content-type:image/jpeg
content-length:83185
Last-modified:tue, Feb 2009 08:01:04 GMT
cache-control:max-age=2592000
Expires:thu, APR 2009 05:14:08 GMT
Etag: "5d8c72a5edda8d6a:3239″
where last-modified,Expires , and Etag are tagged page cache identities

First, last-modified, expires and etag related working principle
1, last-modified
When the browser first requests a URL, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute tag (Http reponse Header), which is the last time the file was modified at the service end, in a format similar to this:
Last-modified:tue, Feb 2009 08:01:04 GMT
The second time the client requests this URL, according to the HTTP protocol, the browser will transfer to the server if-modified-sinceHeader (Http Request header), asking if the file has been modified after that time:
If-modified-since:tue, Feb 2009 08:01:04 GMT
If the server-side resources do not change, the HTTP 304 (notchanged.) Status code is automatically returned, and the contents are empty, thus saving the amount of data transferred. When the server-side code changes or restarts the server, the resource is issued again, similar to the first request. This ensures that resources are not duplicated to the client and that the client is able to get the latest resources when the server changes.
Note: If the if-modified-since time is later than the current time of the server (current request time request_time), it will be considered an illegal request.

2, etag working principle
The HTTP protocol specification defines etag as the entity tag of the requested variable (see 14.19). A simple point is that the request URL is marked when the server responds and is routed to the client in the HTTP response header, similar to the format returned by the server side:
Etag: "5d8c72a5edda8d6a:3239″
The client's query update format is like this:
If-none-match: "5d8c72a5edda8d6a:3239″
If the etag does not change, the status 304 is returned.
That is, after the client makes a request, the Http reponse header contains Etag: "5d8c72a5edda8d6a:3239″
The identity is equal to telling the client side that you get the resources for this to have an indication of id:5d8c72a5edda8d6a:3239. The next time the request asks for the same URI, the browser emits a If-none-match header (Http requestheader) at the same time, the information in the header contains the last access ETag: "5d8c72a5edda8d6a : 3239″ identification.
If-none-match: "5d8c72a5edda8d6a:3239"
, so that the client end equals cache two, the server side will be compared to 2 of the etag. If If-none-match is false and does not return 200, 304 (not Modified) Response is returned.

3, Expires
Given the date/time, the response is considered obsolete. such as Expires:thu, APR 2009 05:14:08 GMT
Need and last-modifiedUsed in combination. Used to control the time that the request file is valid, and when the request data is within the validity period, the client browser requests data from the cache instead of the server side. The data is not determined to be updated from the server when the data in the cache is invalidated or expired.

4, last-modified and expires
The last-modified logo can save a bit of bandwidth, but it's still not going to run out of an HTTP request and be used with expires. And the expires identity makes the browser simply does not have the HTTP request to send, for example when the user F5 or clicks the refresh button, even if has the expires URI, also will send an HTTP request to go out, therefore, last-modified still wants to use, but And to be used together with expires.

5, ETag and expires
If the server side sets both ETag and expires, the etag principle is the same as 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 the last-modified,etag values emitted by webserver, and after the exact match if-modified-since and If-none-match are checked for modification time and ETag, The server can return 304.

6, last-modified and ETag
Used with the HTTP header of the last-modified and ETAGS requests, the server first produces the Last-modified/etag tag that the server can use later to determine whether the page has been modified to determine whether the file continues to cache
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.
Note:
1, last-modified, and ETag headers are HTTP reponse that are issued by the WEB server Header,web server should support both headers.
2, the client will cache these headers after the WEB server sends the LAST-MODIFIED/ETAG headers to the client;
3. When the client initiates the same page request again, the HTTP requestheader:if-modified-since and if-none-match corresponding to the Last-modified/etag will be sent separately. We can see that the values of these two headers are exactly the same as the last-modified,etag values emitted by webserver;
4, through the above value to the server side check to determine whether the file continues to cache;

Two, Apache, lighttpd and nginx pins are configured ETag and expires, effectively caching files such as css/js/pic/pages/streaming media.
A, Expires
A.1, Apache Etag
Set using Apache's Mod_expires module, which includes the Expires header content and the Max-age command of the Cache-control header when the answer is answered
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 minutes"
Expiresbytype text/css "Access plus minutes"
Expiresbytype text/txt "Access plus minutes"
Expiresbytype Text/js "Access plus minutes"
Expiresbytype application/x-javascript "Access plus minutes"
Expiresbytype Application/x-shockwave-flash "Access plus 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, Cache-control max-age information is automatically exported
Detailed information about Expires can be viewed in the official Apache documentation.
In this time period, the request for this file will be retrieved directly from the cache server.
of course, if you need to ignore the browser's refresh request (F5), cache server Squid also needs to use the Refresh_pattern option to ignore the 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$ 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$ 50% Reload-into-ims
Refresh_pattern. 10 50% 60
For instructions on expires in squid, please refer to the Squid official Refresh_pattern introduction.

A.2, Lighttpd Expires
Like Apache LIGHTTPD settings expire also first check to see if the Mod_expire module is supported,
The following setting is to have the files in all images directories in the URI expire after 1 hours;
Expire.url = ("/images/" => "Access 1 Hours")
Here are the files that act on the images directory and its subdirectories;
$HTTP ["url"] =~ "^/images/" {
Expire.url = ("" => "Access 1 Hours")
}
You can also specify the type of file;
$HTTP ["url"] =~ "\. (JPG|GIF|PNG|CSS|JS) $ "{
Expire.url = ("" => "Access 1 Hours")
}
Specific reference LIGHTTPD official expires explanation

A.3, Nginx in expires
Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
{
Expires 30d;
}
Location ~. *\. (JS|CSS)? $
{
Expires 1h;
}
This type of file is not often modified by expires instructions to control its caching in the browser to reduce unnecessary requests. The expires directive controls the headers of the "Expires" and "Cache-control" in the HTTP answer (plays a role in controlling the page cache). Other please refer to Nginx expires

B.1, Apache etag settings
Setting up ETag support in Apache is simpler, using only a single file in a directory containing static files. htaccess, inside Add:
Fileetag Mtime Size
This will be fine, and you can refer to the Apache Fileetag document page in detail.

B.2, Lighttpd Etag
To set up ETag support in LIGHTTPD:
Etag.use-inode: Whether to use Inode as ETag
Etag.use-mtime: Whether to use file modification time as ETag
Etag.use-size: Whether to use file size as ETag
Static-file.etags: Whether to enable ETag features
The fourth parameter must be enabled, the front three to see the actual need to choose, recommend the use of modified time

B.3, Nginx Etag
the ETag identity is not added by default in Nginx.Igor Sysoev view "On the static file processing can not see how to etag better than the Last-modified logo." "
Note:
Yes, it ' s addition,and it's easy to add, however, I did not have to be better than last-modified for static files. -igor Sysoev
A Nice short description are here:
http://www.mnot.net/cache_docs/
It looks to me this 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 module, please refer to
Http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

iii. epires and ETag processing in non-real-time interactive dynamic pages
The data update is not frequent, such as tag classification file, and so on, you can consider its cache. The simple point is to output the expires and etag identities in a dynamic program that is not in real time interaction, so that they are cached. However, you need to pay attention to closing the session to prevent HTTP response when HTTP header contains session ID identification;
3.1, Expires
such as 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 () + ' 86400′ '). ' GMT ');
?>
The above information indicates that the file expires 24 hours after the request.
Other dynamic pages that need to be processed can be called directly.
3.2, Etag
Processed according to the HTTP return status. When you return 304, read directly from the cache
such as 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 a reasonable configuration of ETag can save a lot of bandwidth.
In fact, configuring the ETag cache does not configure the last-modified header effect.
When we do not know the modification time, ETag is still more useful for dynamic content.

Mobile home Gao Chunhui about etag supplement:
A few different or additional places to add:
1. "Of course, the impact of Etag on most site performance is not very large." "It should be said that Etag in the correct use of the case, will allow a large number of requests in 304-way response, can be considerable savings in server resources and bandwidth." Some of the places previously written do not use Etag, is based on some webserver Etag calculation method contains the Inode, which in the case of multiple Web servers can not be used, and change this calculation method.
2, for the early refresh this, PHP is almost impossible to do. Even if you perform flush and similar functions, you will not output to the browser end until the request is fully executed.
3, AJAX use Get and POST each have the benefit, get way can be faster response, but there may be browser caching problems, generally need to add a random number to avoid, POST method will not. So it's best to use the Get and POST methods separately, depending on your 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.