Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Etag and Expires performance tuning

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the method of Etag and Expires performance tuning". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the method of Etag and Expires performance tuning" can help you solve the problem.

1. The working principle of Http Request Header on Client side and Http Reponse Header on Server side in Etag and Expires.

2. Static configuration of Etag and Expires in Apache, Lighttpd and Nginx

3. Etag and Expires processing in non-real-time interactive dynamic pages

When the client requests a certain URL for the first time through the browser, according to the HTTP protocol, the browser will send a header (Http Request Header) to the server, the server will respond and record the relevant attribute tag (Http Reponse Header), and the return status of the server will be 200. the format is similar to the following:

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 the header (Http Request Header) to the server, the server will respond and record that the relevant record attribute tag file has not changed, and the server will return 304 to read directly from the cache:

HTTP/1.x 304 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"

Where Last-Modified, Expires and Etag are tag page cache identities

1. Related working principles of Last-Modified, Expires and Etag

1 、 Last-Modified

When the browser requests a URL for the first time, the server's return status will be 200. the content is the resource you requested, and there will be a Last-Modified attribute tag (Http Reponse Header) when the file was last modified in the server. 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, according to the HTTP protocol, the browser sends the If-Modified-Since header (Http Request Header) to the server, asking if the file has been modified after that time:

If-Modified-Since: Tue, 24 Feb 2009 08:01:04 GMT

If there is no change in the resources on the server side, the HTTP 304 (NotChanged.) status code is returned automatically, and the content is empty, thus saving the amount of data transferred. When the server-side code changes or the server is restarted, the resource is reissued and the return is similar to the first request. This ensures that resources are not repeatedly sent to the client, and that when the server changes, the client can get the latest resources.

Note: if the time of the If-Modified-Since is later than the server's current time (the current request time request_time), it will be considered an illegal request

2. Working principle of Etag

The HTTP protocol specification defines ETag as the "entity tag of the requested variable" (see 14.19). The simple point is that when the server responds, it marks the request URL and transmits it to the client in the HTTP response header, similar to the format returned by the server:

Etag: "5d8c72a5edda8d6a:3239"

The query update format on the client side is as follows:

If-None-Match: "5d8c72a5edda8d6a:3239"

If the ETag has not changed, the status is returned to 304.

That is, after the client makes a request, the Http Reponse Header contains Etag: "5d8c72a5edda8d6a:3239"

Logo, which is tantamount to telling the client that the resource you got represents ID:5d8c72a5edda8d6a:3239. The next time you need to send a Request to request the same URI, the browser also sends out an If-None-Match header (Http RequestHeader). The information in the header contains the Etag: "5d8c72a5edda8d6a:3239" logo obtained from the last visit.

If-None-Match: "5d8c72a5edda8d6a:3239"

In this way, the client side equals two copies of the Cache, and the server side will compare the etag of the two. If If-None-Match is False, do not return 200,304 (Not Modified) Response is returned.

3 、 Expires

The date / time given is considered obsolete by the response. For example, Expires: Thu, 02 Apr 2009 05:14:08 GMT

It needs to be used in conjunction with 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. 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 logo can save a little bit of bandwidth, but it still can't escape sending a HTTP request, and it needs to be used with Expires. On the other hand, the Expires logo allows the browser not to send a HTTP request at all. For example, when a user clicks the Refresh button or F5, a HTTP request will be sent even for the URI with Expires, so Last-Modified is still used and should be used with Expires.

5. Etag and Expires

If both Etag and Expires are set on the server side, the principle of Etag is the same, that is, HttpRequest Header:If-Modified-Since and If-None-Match corresponding to Last-Modified/Etag. We can see that the values of these two Header are exactly the same as the Last-Modified and Etag values issued by WebServer; the server can only return 304. 4 after exactly matching If-Modified-Since and If-None-Match, that is, after checking the modification time and Etag.

6. Last-Modified and Etag

Last-Modified is used together with the http header of the ETags request. The server first generates the Last-Modified/Etag tag, which the server can use later 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).

two。 The server returns page An and adds a Last-Modified/ETag to A.

3. The client displays the page and caches the page along with Last-Modified/ETag.

4. The client requests page An again and passes the Last-Modified/ETag returned by the server on the last request to the server.

5. The server examines the Last-Modified or ETag, determines that the page has not been modified since the last client request, and directly returns a response of 304 and an empty response body.

Note:

1. Both Last-Modified and Etag headers are issued by Web Server. Http Reponse Header,Web Server should support both headers.

2. After Web Server sends Last-Modified/Etag headers to the client, the client will cache these 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 Last-Modified/Etag will be sent respectively. We can see that the values of these two Header are exactly the same as the Last-Modified and Etag values issued by WebServer.

4. Check the above values to the server to determine whether the file continues to be cached.

Second, Etag and Expires are configured in Apache, Lighttpd and Nginx to effectively cache pure static files such as css/js/pic/ pages / streaming media.

A 、 Expires

A.1 、 Apache Etag

Use the mod_expires module of Apache to set it, which includes the max-age instruction that controls the content of the Expires header and the Cache-Control header when the Dd is expected.

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

ExpiresActive on

ExpiresDefault "access plus 1 year"

When expires is set, the max-age information of Cache-Control is automatically output

For more information about Expires, please see the official Apache documentation.

During this time period, requests for the 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 28800 ignore-reload

Refresh_pattern-I\ .jpg$ 1440 28800 ignore-reload

Refresh_pattern-I\ .jpeg$ 1440 28800 ignore-reload

Refresh_pattern-I\ .png$ 1440 28800 ignore-reload

Refresh_pattern-I\ .bmp$ 1440 28800 ignore-reload

Refresh_pattern-I\ .100% htm$ 60 ignore-reload

Refresh_pattern-I\ .html$ 1440 28800 ignore-reload

Refresh_pattern-I\ .xml$ 1440 28800 ignore-reload

Refresh_pattern-I\ .txt$ 1440 28800 ignore-reload

Refresh_pattern-I\ .css$ 1440 28800 reload-into-ims

Refresh_pattern-I\ .js$ 60 50% 100 reload-into-ims

Refresh_pattern. 10 50% 60

For instructions on Expires in Squid, please refer to the introduction of refresh_pattern in the official Squid.

A.2 、 Lighttpd Expires

Like Apache, Lighttpd needs to check whether the mod_expire module is supported before setting expire.

The following setting is to make all files in the images directory in URI expire in 1 hour.

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")

}

Please refer to the official Expires explanation of Lighttpd.

A.3. Expires in Nginx

Location ~. *\. (gif | jpg | jpeg | png | bmp | swf) $

{

Expires 30d

}

Location. *\. (js | css)? $

{

Expires 1h

}

Such files are not often modified, and their caching in the browser is controlled by expires instructions to reduce unnecessary requests. The expires instruction can control the headers of HTTP should Dd Expires "and" Cache-Control "(play the role of controlling page cache). For more information, please see Expires in Nginx.

B.1, Etag settings in Apache

It is relatively simple to set up Etag support in Apache. You only need to create a file .htaccess in the directory containing static files, and add:

FileETag MTime Size

This is fine. For details, please refer to the FileEtag document page of Apache.

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 to enable the function of Etag

The fourth parameter must be enable. The first three should be selected according to the actual needs. It is recommended to use the modification time.

B.3 、 Nginx Etag

By default, Nginx does not add the view that the Etag logo .Igor Sysoev "does not see how Etag is better than the Last-Modified logo 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 is also a third-party nginx-static-etags module, please refer to

Http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

Third, for Epires and Etag processing in non-real-time interactive dynamic pages

Data is not updated frequently, such as tag classification and archiving, and so on, so you can consider cache it. The simple point is to output expires and etag identities in dynamic programs that do not interact in real time and let them be cached. However, you need to be careful to turn off session to prevent the http header from including the session id logo when http response.

3.1 、 Expires

Such as expires.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report