Content cache expiration mechanism for IIS effectively improve site performance _win Server

Source: Internet
Author: User
Tags http request
Our site often contains a large number of page components, such as pictures, style sheet files, JS script files and Flash animation. These components have a very low frequency of change, especially those components that make up the basic framework of the Web site,

Hardly ever change. We can treat these very low rate components as static content, using IIS's content expiration mechanism and browser's local caching mechanism to keep them on the visitor's computer hard disk for a period of time.

When a visitor visits your site, if the local static content does not expire, the browser mounts from the local hard drive without making a request to the server.

If you use tools such as Fiddler to track Web Access, you will see clearly that although only one page is accessed, there are more than one HTTP request and response. Each picture in the page, each

JS script files, each CSS file, will raise a request and answer. Therefore, if you want to make the page access faster, reduce the number of HTTP requests, reduce the number of downloads from the server is an effective way.

The use of content expiration mechanism can be achieved after this goal, this is the use of content expiration mechanism.

Most Web developers have played IIS 6 or IIS 7, but how many of them have looked at the contents of the HTTP headers or HTTP Response headers tags? Here I take IIS 6 for example,

By default, the interface in this tab is shown in the following illustration:

At this point, if a request is made to a Web page on the Web page that contains a link to a picture, the browser continues to make a request to the page after the HTML document is fetched, and the response to the request is expressed in the HTTP Response header as follows:

http/1.1 OK (indicates that the server found the picture and responded correctly)

Date:thu Feb 08:25:38 GMT (response time, GMT)

Last-modified:wed, 2009 01:55:06 GMT (picture Last modified time, GMT)

This image will be saved by the browser in the IE Temp folder on your local hard drive. Use the same browser window to access this page again in the same session,
The components on the page are not requested again.

When you open another browser window (another session) on this machine and then visit this page again, because this picture is already saved locally, the browser
There is no expiration mechanism for the content in the response just now, so the browser will still send a request to the server:
If-modified-since:wed, 2009 01:55:06 GMT (Ask the server, my local picture of the last modification time is this, after this time you have no updated version? )
if-none-matched: "ABDKFKDKDKDJKJKFKFD" (This is a etag encoding that is the only indication that the server is to the component)

After the server received the request to check the requested picture, found that its most recent modification time or Wed, 2009 01:55:06 GMT, then respond to the request:
http/1.1 304 Not Modified (the requested picture was found, and has not been changed)

Date:thu Feb 08:25:38 GMT (response time)
When the browser receives this response, it knows that it can safely use the locally stored image and no longer has to download the component from the server.

This shows that the default setting for the IIS Http headers label is not to prevent browser caching, but it does not tell the component to save an expired time, so the browser saves the component locally,
Each visit asks the server if the component is out of date, and if it does not expire, uses locally saved content or downloads the content from the server. You can see that it only reduces the number of downloads of content from the server.
There is no reduction in the number of requests made to the server, and requests and responses are still time-consuming.

Navigate to the folder where the Web site holds pictures in IIS, and then open the Properties window and make the following selections in HTTP headers that the component's expiration time is 1 days after this request, which is 86,400 seconds cached locally.

Open the browser for the first time to visit a Web page that contains a link to a picture, so the response to the picture request is expressed in the HTTP Response header as follows:

http/1.1 OK (indicates that the server found the picture and responded correctly)
Cache-control:max-age=86400 (allows the picture to be cached locally for 86,400 seconds from the time of this request)

Date:sat 08:09:29 GMT (response time, GMT)
So, as long as within 1 days, using the native browser to open the Web page, will not make a request for this picture, but directly use the local cache of this picture. Visible, reduces unnecessary HTTP requests,

Improve the response speed of web pages.

Many Web site framework components are permanent, so we can set longer expiration times, as follows:

Open the browser for the first time to visit a Web page that contains a link to a picture, so the response to the picture request is expressed in the HTTP Response header as follows:
http/1.1 OK (indicates that the server found the picture and responded correctly)
Date:sat 08:50:12 GMT (response time, GMT)
Expires:mon 16:00:00 GMT (local cache of the picture until 16 o'clock, GMT, May 23, 2011)
This means that you will no longer be asked to make a request to this picture as long as the page is accessed on this computer before 16 o'clock on May 23.

Some people worry that if you set the expiration mechanism this way, once the components have been updated, visitors will not be able to receive changes, it is also regrettable. In fact, there are two ways to solve this problem:
On the one hand is the Web site developers, should be the picture, style sheet files and JS file naming methods to improve, such as the file name to add version number, so that once you modify the component content,
The component should have a new name, so the browser will find that the component has not been cached locally and will naturally initiate the request.
On the other hand, visitors can force a request to be restarted on a component in a Web page through the Refresh feature of the browser. Even if an expiration mechanism is set, the refresh feature of the browser will still affect all page components
Making the request.

In summary, the purpose of this article is to illustrate the interaction between the browser's local cache and the Web server cache expiration mechanism, and how to achieve performance improvement in this way.
According to the "High Performance website Construction Guide" in the book, from the browser to send a request to a Web page, the time to get the HTML Document page is only the entire page answer completion time
5%, and the remaining 95% of the time are all components in the request and download pages. Therefore, to reduce the request and download of the components in the page, it is very meaningful to use the browser caching mechanism effectively.

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.