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/htmlCode 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 based on PHPProgramSo 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 interaction between B and S, reduce bandwidth traffic and 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 codeCopy 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 codeCopy 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 codeCopy 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 codeCopy 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.