How to Use HTTP cache to improve webpage Performance

Source: Internet
Author: User
Tags metabase

The Performance of a website must be considered in two aspects: the operational efficiency on the Server side and the efficiency on the Client side. Do not think that the Server is highly efficient. This website or service is highly efficient for the user experience. If you do not consider the factors that affect Client Performance, the user may feel as slow as running on the Server.

The research done by Yahoo's buddies is really not covered. They also wrote a book "High Performance Web Sites" to describe how to improve Client Performance. There is also a Team inside the company to do this, and they have heard their training, which is similar to what Yahoo says. The so-called heroes are slightly different.

Add Repsone for static resources (HTML files, image files, etc.)Expires/Cache-ControlHeader is a very valid trick. If the HTTP Response contains headers such as Expires, the browser will Cache this resource. Ideally (note, it is only ideal), before the Expire Date, no more HTTP requests will be sent to the Server for this resource, but the Expires value can only be a fixed date, such as "Thu 27 Nov 2008 07:00:00 GMT ", it cannot be a random floating value similar to "10 years from now on". If you want this effect, you can use headers such as Cache-Control, if the HTTP Resposne contains the Header "Cache-Control: max-age = 100", the maximum service life of this resource in the cache is 100 seconds. Generally, this static file should never expire. If you want to add a period to the Cache, I hope it will be 10 thousand years, "Cache-Control: max-age = 315360000000 ";

In this way, the new and old web pages can work at the same time, but it is not in order to save storage space to overwrite the original files.

For the Apache server, use mod_expires to add it to httpd. conf or. htaccess.

<FilesMatch "\\.(ico|gif|jpg|html)$">
ExpiresDefault "access plus 10 years"
</FileMatch>

For IIS 6 (IIS 7 is not clear yet), you can use IIS Manager to add Expires/Cache-Control through the GUI. It is troublesome to modify the Control through the command line.

First, go to the AdminScripts directory of IIS and find the adsutil. vbs file.

Cd C: \ Inetpub \ AdminScripts

For example, to add Expires/Cache-Control to the imags directory under the root directory, add a node to metabase.

Cscript adsutil. vbs create W3SVC/1/root/images "IisWebVirtualDir"

If you want to access the files in the images directory to obtain Cache-Control: max-age = 60

Csripadsutil. vbs set W3SVC/1/root/images/HttpExpires "D, 0x3c"

If you want to access the file in the images directory to obtain "Expires: Thu 27 Nov 2008 07:00:00 GMT ",

Csripadsutil. vbs set W3SVC/1/root/images/HttpExpires "S, Thu 27 Nov 2008 07:00:00 GMT"

In addition, the same HTTP Response can have both Expires and Cache-Control, but the Cache-Control permission is higher than Expires and override it.

There will be another Header namedLast-ModifiedFor example, "Last-Modified: Thu, 06 Apr 2006 21:17:12 GMT", after the browser accesses a URI and obtains such a Resposne, it will know the Last modification time of the resource, next time you need to obtain this resource again, a Request will be sent to the Server, but there is a "If-Unmodified-Since: Thu, 06 Apr 2006 21:17:12 GMT" in this Request ", if the Server modifies the resource after this date, the resource will be returned to the Client as usual. However, if no modification is made, a 304 (Not Modified) error will be returned) response, instead of returning resources, told the Client: "This resource has never been changed since it was last provided to you. You can use it in your Cache." A 304 Response is usually much smaller than a static resource, which saves network bandwidth.

Let's look back and compare the two items Expires and Last-Modified. It seems that Last-Modified is inferior to Expires, because although it can save a little bandwidth, it still cannot escape and send an HTTP request, however, Expires makes it hard for the browser to send HTTP requests! What should I do with the Last-Modified object? The ideal situation is true, but when you press F5 in IE or Firefox or click the Refresh button (not re-enter the URL in the URL bar and press Enter ), even for URIs with Expires, an HTTP request is sent. Therefore, the Last-Modified must be used together with Expires.

BesidesLast-Modified, HTTP Response may also have another Header:ETagTo make the static resources on the Server taste a little "version control". If the HTTP Response contains "ETag:" abcdefg1234: 0001 ", it is equal to telling the Client, the resource you get for this version has an ID named abcdefg1234: 0001. When you need to send a Request to Request the same URI next time, add a "If-None-Match: "abcdefg1234: 0001" ", well, some modifications have been made on the Server side. Next time this Client receives a request, but the resource has been changed, so this new resource is returned, there is also a new tag "ETag:" abcdefg4567: 0001 "(I wrote this etag). In this way, the Client has two copies of the Cache. When you need to request this resource, it can contain the following Header: "If-None-Match:" abcdefg1234: 0001 "" abcdefg4567: 0001 "", In this case, even if the Server side is hot-headed and the resource is rolled back to the original version, the system will still return the 304 (Not Modified) Response because it knows that the Client side caches the previous version, this feature cannot be implemented by Last-Modifed/If-Not-Modified.

However, the ETag/If-None-Match feature is really a weakness. First, Server resources are unlikely to Roll Back. More importantly, it may cause a decline in Client Performance. For websites with only one Server, there is no problem, but now all websites with a slightly higher Scale need to Scale Out. That is to say, a Load Balancer at the front end needs to be followed by multiple servers to process requests, generally known as Cluster, since it is a Cluster, it is irrelevant to the Server to which each request returns results. However, this ETag may be a bad thing. If the user's first request is assigned to Server A, "ETag:" abcdefg1234: 0001 "" is returned, but the second request is assigned to Server B, the resource on Server B is the same as that on Server A, but the ETag calculated for this resource is "abcdefg1234: 0002". This is troublesome. Although the content is the same, the ETag does not match, I wasted bandwidth and sent the resources again! In fact, ETag on different servers may be different. For Apache, ETag takes into account inode for computing, and for IIS, ETag takes into account the modified metabase version, it is a little difficult to ensure that the information on different servers is consistent. But isn't there a Last-Modified/If-Not-Modified? If-Modified-Since is displayed on the Server end, the Server can directly send back 304 (Not Modified) Regardless of If-None-Match. Unfortunately, RFC2616 specifies this situation. If there is both If-None-Match and If-Modified-Since, no 304 is returned unless there is no conflict between the two.

Therefore, ETag is harmful. According to Yahoo's suggestion, do not try to synchronize ETag on different servers. Delete the ETag (default, apache and IIS both have ETag). I Sniff a few Yahoo webpages and returned HTTP Response. There is indeed no ETag, and people are indeed in the unity of knowledge and practice;

For Apache, add a line in httpd. conf or. htaccess:

FileETag none

For IIS 6, it is a little difficult. First, it seems that there is no way to remove ETag through Config. I checked a lot of information and asked many people, it seems that the only way to remove ETag is to write an ISAPI Filter. Sniff looked at several Microsoft Web pages and the results showed that ETag was stable. It is estimated that there is really no good way at present.

We had to leave the second place to ensure that the ETag on different servers is consistent. The Etag calculation algorithm of IIS is ETag = {Filetimestamp: ChangeNumber}, and the Filetimestamp is consistent. ChangeNumber is the change number of metabase, it is a little difficult to ensure that every Server in the Cluster is the same, so simply set it to a fixed value. This connection tells us what to do. Unfortunately, the ETags configuration cannot be completely deleted.

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.