Last-modified and ETag and configuration in Apache and Nginx

Source: Internet
Author: User
Tags session id

1) What is "last-modified"?
When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the end of the service period, similar in format:
Last-modified:fri, 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:
If-modified-since:fri, 2006 18:53:33 GMT
If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.
   2) What is an "Etag"?
The HTTP protocol specification defines the etag as the entity value of the requested variable (see-section 14.19). Another argument is that the ETag is a 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 judging what the token is and what it means, and transmitting it to the client in the HTTP response header, which is the format returned by the server side:
ETag: "50b1c1d4f775c61:df3"
The client's query update format is this:
if-none-match:w/"50b1c1d4f775c61:df3"
If the etag does not change, it returns the status 304 and does not return, which is the same as last-modified. I test the etag is mainly in the breakpoint download is more useful.
   how do last-modified and etags help improve performance?
Smart developers will use the HTTP header for Last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates the LAST-MODIFIED/ETAG tag, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate its (client) cache by passing the token back to the server.


The process is as follows:
1, the 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 Last-modified/etag.

4, the Customer requests page A again and passes the Last-modified/etag that the server returned when the last request was sent 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, directly returns the response 304 and an empty response body.

------------------------------------------------------------------------------------------------

Proper use of etag and expires identity processing can make the page more efficient to cache.
When a client requests a URL for the first time through a browser, the browser transmits a header (HTTP Request header) to the server, as specified in the HTTP protocol, and the server-side response records the associated attribute tag (HTTP reponse header). The return status of the server side will be 200, similar to the following format:
http/1.1 OK
Date:tue, 04:58:40 GMT
Content-type:image/jpeg
content-length:83185
Last-modified:mon, 16:29:24 GMT
cache-control:max-age=2592000

Expires:thu, 05:14:08 GMT
Etag: "When the Xok.la-961aa72-4cea99b4415628″ client requests this URL for the second time, the browser transmits a header (HTTP Request header) to the server according to the HTTP protocol. Server-side response and record related records attribute tag file is not changed, server side returns 304, read directly from cache:
http/1.x 304 Not Modified
Date:tue, 05:03:56 GMT
Content-type:image/jpeg
content-length:83185
Last-modified:mon, 16:29:24 GMT
cache-control:max-age=2592000
Expires:thu, 05:14:08 GMT
ETag: "Xok.la-961aa72-4cea99b4415628″ where last-modified, expires, and ETag are tagged page cache identities


I. Last-modified, expires and etag related working principles
1, last-modified
When the browser requests a URL for the first time, 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) This file was last modified at the end of the service period, in the format similar to this:
Last-modified:mon, 16:29:24 GMT when the client requests this URL for the second time, the browser transmits the If-modified-since header (HTTP Request heade) to the server, as specified in the HTTP protocol. R), ask if the file has been modified after that time:
If-modified-since:mon, 16:29:24 GMT if the server-side resource does not change, the HTTP 304 (notchanged.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the 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 (the current request time Request_time), it will be considered an illegal request.
2. ETag working principle
The HTTP protocol specification defines the etag as the entity tag of the requested variable (see 14.19). The simple point is that the server responds with the request URL token and transmits it to the client in the HTTP response header, similar to the format returned by the server side:
Etag: "The query update format for the Xok.la-961aa72-4cea99b4415628″ client is this:
If-none-match: "Xok.la-961aa72-4cea99b4415628″ returns the status 304 if the ETag has not changed.
That is: After the client makes the request, the Http reponse header contains the Etag: "Xok.la-961aa72-4cea99b4415628″
The identity, which is equal to telling the client side, you get this resource has the expression id:xok.la-961aa72-4cea99b4415628. The next time you request a request for the same URI, the browser issues a If-none-match header (Http Requestheader) that contains the etag that was last accessed in the header: " Xok.la-961aa72-4cea99b4415628″ identification.
If-none-match: "xok.la-961aa72-4cea99b4415628", so that the client side equals the cache two copies, the server side will be compared to 2 of the etag. If If-none-match is false, returns 200, returning 304 (not Modified) Response.
3, Expires
Given the date/time after the response is considered obsolete. such as Expires:thu, 05:14:08 GMT
Must be used in conjunction with the last-modified. Used to control the validity time of the request file, when the request data is within the validity period, the client browser requests data from the cache instead of the server side. When data in the cache is invalidated or expired, the data is updated from the server.
4, Last-modified and expires
The Last-modified logo can save a bit of bandwidth, but still can't get out of an HTTP request and use it with expires. And the Expires logo makes the browser simply even HTTP requests do not have to send, such as when the user F5 or click the Refresh button even for a expires URI, the same will send an HTTP request out, so, last-modified still need to use, and And be used with expires.
5. ETag and expires
If both the ETag and the expires are set on the server side, the ETag principle is the same as the HttpRequest header:if-modified-since and if-none-match corresponding to the Last-modified/etag. We can see that the value of these two headers is exactly the same as the Last-modified,etag value emitted by the webserver, and after the exact match if-modified-since and If-none-match are examined, the modification time and the ETag are The server can return 304.
6, Last-modified and ETag
Last-modified is used with the HTTP header of the etags request, the server first generates the Last-modified/etag tag, which the server can later use to determine whether the page has been modified to determine whether the file continues to be cached
The process is as follows:
1. The 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 Last-modified/etag.
4. The Customer requests page A again and passes the Last-modified/etag that the server returned on 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, directly returning response 304 and an empty response body.
Note:
1, last-modified, and ETag headers are all HTTP reponse that are emitted by the WEB server Header,web server should support both headers.
2. After the WEB server sends the LAST-MODIFIED/ETAG header to the client, the client caches the headers;
3. When the client initiates a request for the same page 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 the two headers are exactly the same as the last-modified,etag values emitted by the webserver;
4, through the above value to the server-side check to determine whether the file continues to cache;


Second, Apache, lighttpd and nginx pin configuration etag and expires, effectively cache pure static such as css/js/pic/page/stream media and other files.
A, Expires
A.1, Apache Etag
Use Apache's Mod_expires module to set, which includes controlling expires header content at the time of the reply and max-age instructions 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 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" or "minutes" or


Expiresactive on
ExpiresDefault "Access plus 1 year"

Cache-control max-age information is automatically output when expires is set
Detailed information about Expires can be seen in the official Apache documentation.
During this time period, requests for 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 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 squid expires, please refer to Squid's official Refresh_pattern introduction.
A.2, Lighttpd Expires
Same as Apache LIGHTTPD setting expire also first to see if the Mod_expire module is supported,
The following settings are allowed to expire files in all images directories in the URI after 1 hours;
Expire.url = ("/images/" + "Access 1 hours") below is a file that lets you work 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 Expireslocation ~. *\. (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 cache in the browser to reduce unnecessary requests. The expires directive controls the header of "expires" and "Cache-control" in the HTTP response, which is used to control the page cache. Other please refer to the nginx expires
B.1, Apache the ETag setting in Apache the support for setting the ETag is relatively simple and is only used to create a file in a directory containing static files. htaccess, add:
Fileetag MTime Size This is OK, detailed reference to the Apache Fileetag document page
B.2, Lighttpd ETag set 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 the ETag feature is enabled
The fourth parameter is definitely to enable, the first three to see the actual need to choose it, recommend the use of modified time
B.3, Nginx Etag
The ETag identity is not added by default in Nginx. Igor Sysoev's View "There is no way to see how the ETag is better than the last-modified identity on static file processing." ”
Note:
Yes, it's Addition,and it's easy to add, however, I don't see Howetag are better than last-modified for static files. -igor Sysoev
A Nice short description are here:
http://www.mnot.net/cache_docs/#WORK
It looks to me so 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 to
Https://github.com/mikewest/nginx-static-etags
Third, for non-real-time interactive dynamic page expires and ETag processing
The data update is not frequent, such as tag classification archive, etc., you can consider to its cache. The simple point is to output the expires and ETag identifiers in non-real-time interactive dynamic programs and let them cache. However, you need to pay attention to closing the session, to prevent HTTP response when the HTTP header contains session ID identification;
3.1, Expires
such as expires.php

Last-modified and ETag and configuration in Apache and Nginx

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.