Analysis of the Principle and Application of HTTP Cache Protocol in Apache

Source: Internet
Author: User

Etag is also available for static pages.

I. First, let's look at the first situation: apache Static Page

The static pages sent from apache to the client generally contain Last-Modified and Etag. The values of these two labels come from the modification time of the static file and inode.

The following is the header of the apache return client.

XML/HTML code
Copy codeThe Code is as follows:
Last-Modified: Fri, 26 Jan 2007 01:53:34 GMT
ETag: "3f9f640-318-cb9f8380"

The reason why search engines like static files is that they have these two identifiers, which can be used to determine whether files have been updated.

2. PHP and other dynamic pages

Because php is dynamically generated, its content cannot be determined based on the time of the php program, so any Cache control is included when php returns the client by default, to make good use of the cache, you must understand the cache mechanism, and reduce the interaction between B and s, reduce bandwidth traffic, and reduce the server load... there are many benefits.

Iii. Definition of Cache Control

First, explain the meanings of the labels I have tested and understood.

Cache-Control: Specifies the Cache mechanism for requests and responses. Setting Cache-Control in a request message or response message does not modify the Cache processing process of another message. The cache commands in the request include no-cache, no-store, max-age, max-stale, min-fresh, only-if-cached, commands in the Response Message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, and max-age.

The instructions in each message are as follows:

Public indicates that the response can be cached in any cache area.

Private indicates that the whole or part of the response message of a single user cannot be processed by the shared cache. This allows the server to only describe part of the user's response message, which is invalid for requests of other users.

No-cache indicates that the request or response message cannot be cached.

No-store is used to prevent the unintentional release of important information. Sending a request message does not cache the request and response messages.

Max-age indicates that the client can receive responses with a lifetime not greater than the specified time (in seconds.

Min-fresh indicates that the client can receive a response whose response time is earlier than the current time plus the specified time.

Max-stale indicates that the client can receive response messages beyond the timeout period. If the value of the max-stale message is specified, the client can receive response messages that exceed the timeout period.

Php usage:

Use header () before output (if ob_start () is used, the header can be placed anywhere in the Program)

PHP code
Copy codeThe Code is as follows:
Header ('cache-Control: max-age = 8 ');

Max-age = 8 indicates the maximum lifetime is 8 seconds. If the browser exceeds 8 seconds, it must go to the server to re-read the data. This time is based on the user's page reading time, and Expires is the absolute time.

Expires: indicates the absolute cache expiration time. If the cache Expires at the specified time point, the browser will not recognize the cache and request the server for the latest one.

Last-Modified: The Last modification time of the document. Its usage is: 1.

If it is a static file, the client sends the time in its cache, and apache compares it. If no modification is found, a header is directly returned. The status code is 304, and the number of bytes is very small, (In the advanced version, compare Etag is added to determine whether the file is changed)

2 php dynamic files:

When the client sends a comparison time, php will determine whether to modify it. If the modification time is the same, only 1024 bytes will be returned. It is not clear why 1024 is returned. If the file generated by your php is very large, it only returns 1024, which saves the bandwidth. The client will automatically display the information in the cache file based on the modification time sent by the server.

Note: without the Last-Modified header, Cache-Control and Expires can also work, but each request must return the actual number of file bytes, instead of 1024.

4. HOW?

You don't need to worry about static pages. If you want to better control the cache of static pages, apache has several modules that can be well controlled. We will not discuss them here.

Php page:

There are two types:

1. infrequently modified pages, similar to news releases. Features of such pages: there will be several changes after the first release, which will not be modified over time. The control policy should be: 1. Send the Last-Modified message for the first time, set the max-age to one day, update the Last-Modified after modification, and the max-age time is normal with the number of modifications. This seems complicated, and it also needs to record the number of modifications. It can also be predicted that the next possible modification time of the token will expire at the specified time specified by Expires.

PHP code
Copy codeThe Code is as follows:
// Header ('cache-Control: max-age = 000000'); // Cache for one day
Header ('expires: Mon, 29 Jan 2007 08:56:01 gmt'); // specify the expiration time
Header ('Last-Modified :'. gmdate ('d, d m y 01:01:01 ', $ time ). 'gmt'); // Greenwich Mean time. $ time is the timestamp when the file is added.

2. frequently changed pages

Similar to bbs and Forum programs, this type of page update speed is relatively fast. The main function of caching is to prevent users from refreshing the list frequently, resulting in the burden on the server database. It is necessary to ensure the timeliness of updates, make sure that the cache can be used.

Here, Cache-Control is generally used to Control, and max-age is flexibly controlled based on the forum posting frequency.

PHP code
Copy codeThe Code is as follows:
Header ('cache-Control: max-age = 60'); // Cache for one minute
Header ('Last-Modified :'. gmdate ('d, d m y 01:01:01 ', $ time ). 'gmt'); // Greenwich Mean time. $ time is the last update timestamp of the post.

Five additional

1. Refresh, go, and force refresh

Some browsers support force refreshing pages with ctrl + F5. What are the differences between them?

To transfer: the user clicks the link and transfers it to it. It uses the cache mechanism completely. If there is a Last-Modified, it will not communicate with the server. You can use the packet capture tool to check that the sending byte is 0 bytes, if the cache expires, it will perform the F5 refresh action.

Refresh (F5): This refresh is determined based on whether the cache has Last-Modified. If there is any refresh, it will be transferred to 304 or 1024 (php). If there is no Last update time, it will be read from the server, returns the actual document size.

Force Refresh: the cache mechanism is completely discarded. The server reads the latest documents and sends the following headers to the server:

XML/HTML code
Copy codeThe Code is as follows:
Cache-Control: no-cache

2 debugging tools

Httpwatch pro is a good tool for viewing the interaction between the browser and the server. The current version is 4.1, and ie7 is supported.

There are other proxy packet capture tools for analysis, http debugging. It has never been used. There is also the tcp packet capture tool, which comes with the network 2000. In addition, there is a tcp packet capture tool. The network monitor that comes with 2000 is not especially difficult to use for http.

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.