When we enter a URL through a browser, and then the server responds to our content, the server has a very complex processing process. The server does not always read the page content on disk based on the page we requested, and then returns the content to the browser for our browsing. If so, when the browser's concurrent requests are particularly large, it can cause a heavy load on the server. So how can servers maintain responsiveness when concurrent requests are particularly large? To solve this problem, the HTTP protocol introduces the cache standard. The Apache httpd server implements this standard for the HTTP protocol. Of course, the request and response are bidirectional, so the cache is not just on the server side of the implementation, the current mainstream browser has also implemented the client cache this function. In this way, when the browser enters the URL, if the contents of the server page is not updated, the browser will immediately show the local cached page to the user browsing, there is no need to request the server. If the client cache content expires, the browser sends a request to the server, then the server can further determine whether the requested page content has changed, if there is no change, and do not have disk operations to read the page content, but directly return a special status code to the client, When the client receives this status code, it directly takes the local cache and displays it to the user. Therefore, in a request process, the client and the server side will be a series of processing of the page, in order to reduce the volume of requests, reduce the amount of data transmission, to reduce server load, speed up response.
In our request, some request is to request a static page, such as input http://ezmonitor.cn/wx1.jpg, this is a static picture, request 100 times or display the same content. This request we invent a word for ourselves, called "Static Request". Another request is to request a dynamic page, such as input http://ezmonitor.cn/dt.php?arg=123, the page returns 123, input http://ezmonitor.cn/dt.php?arg=456, the page returns 456, Although the request dt.php this page, but the parameters are different, the results are different, this request, we invented a word, called "Dynamic Request", in the client cache we mentioned, we can only cache static requests, and not cache dynamic requests. So when a dynamic page is requested, it is bound to generate a request link, and if the page is not error, it will inevitably return 200 code, but dynamic content can not be cached? The answer is yes, but it is cached on disk, only to reduce the time of the server to process the code and reduce I/O operations, not to eliminate the request time and the amount of data transmission. Below, we will discuss the client cache and server-side caching.
Client-Side Caching
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang /zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6D/C9/wKioL1VsDKGzgeLhAACil4tov3Y581.jpg "title=" Clipboard.png "alt=" Wkiol1vsdkgzgelhaacil4tov3y581.jpg "/>
The first time the browser requests the server, the HTTP header information is like this
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/6d/cd/wkiom1vscywtlkdqaafeebx2c44190.jpg "title=" Clipboard.png "alt=" Wkiom1vscywtlkdqaafeebx2c44190.jpg "/>
As you can see, there is nothing special, just an ordinary request, only some simple header information, such as user-agent,accept-encode:xxxx,host:xxxx, which are irrelevant to the cache we are discussing today, and can be ignored.
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/6d/c9/wkiol1vsdnhq8t5laab6q5nwkvo643.jpg "title=" Clipboard.png "alt=" Wkiol1vsdnhq8t5laab6q5nwkvo643.jpg "/>
The first time the server returns to the browser (hereinafter referred to as the client), the HTTP header information is like this,
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang /zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650) this.width=650; " Src= "Http://s3.51cto.com/wyfs02/M02/6D/C9/wKioL1VsDO-j5IMCAAHs5s5SmF0545.jpg" title= "Clipboard.png" alt= " Wkiol1vsdo-j5imcaahs5s5smf0545.jpg "/>
In the HTTP header returned by the server for the first time, the place marked with a red arrow is something we should be concerned about. The first is the etag tag, which is a tagged value that the server calculates for a client-requested file, and the ETag changes when the requested file content changes. The server gives the ETag in the HTTP response, and the client saves the ETag. Cache-control:max-age This token is the server cache expiration time, if the client requests the same page again within 30 seconds, the server will use cached data to respond to the client (this value may not be valid in a static request, because it is possible that the server does not need to fetch the data to the client at all). Expires this token indicates the client's local cache time, and if the browser requests the page, the browser must send the request to the server, or the browser will not generate the request and fetch the local cache data directly.
Ok, next, we'll make a second request, and you'll see the caching processing mechanism.
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/6d/cd/wkiom1vsc27dkebsaacsoaqehow242.jpg "title=" Clipboard.png "alt=" Wkiom1vsc27dkebsaacsoaqehow242.jpg "/>
There are three ways to request the second time,
The first way: in the case of IE browser, if you enter the URL after the input, then the browser will check the last server response back to the expires tag, if the current time in the expires range, then the client will directly take the local cache, do not generate the request. If you are using IE under Win7, the directory of this cache file is C:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet files here, Here you can find the index.html cache file. When the expires request expires, the client generates a link to the server with the requested HTTP header information as follows:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang /zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650) this.width=650; " Src= "Http://s3.51cto.com/wyfs02/M00/6D/C9/wKioL1VsDRvhkaiMAAG3JfjGqp4566.jpg" title= "Clipboard.png" alt= " Wkiol1vsdrvhkaimaag3jfjgqp4566.jpg "/>
At this point, the client passes two cache-related tokens to the server, one is if-modified-since, and the value of this tag is the last modified time of the client's local cache. After receiving the server will be based on the time and the modified time of their own files, if the time is consistent, will return 304 code to tell the client to take the local cache directly. If the time is inconsistent, the 200 code and new content will be returned to the client, and the client will re-establish the local cache. Another is If-none-match, the value of this flag tells the server last received ETag value, after the server receives, this value is compared with the latest ETag value of the file, if the same, return 304 code tells the client to fetch the local cache directly. If the time is inconsistent, the 200 code and new content will be returned to the client, and the client will re-establish the local cache. Here, if if-modified-since is inconsistent with the modification time of the files within the server, but the value of the etag is consistent, and the server will still return 304, at which point the client still takes the local cache directly to the user. Therefore, in the case of an etag tag, the server will only judge the value of the ETag comparison. The following is the response information header for the server when the second request:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang /zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650) this.width=650; " Src= "Http://s3.51cto.com/wyfs02/M01/6D/CD/wKiom1VsC52h0crrAAEuvvmadGo703.jpg" title= "Clipboard.png" alt= " Wkiom1vsc52h0crraaeuvvmadgo703.jpg "/>
The second way: if the user after entering the URL, click "Refresh", then the client will not judge expires tag, and directly request the server. At this point, the server will decide to return 304 or 200 in the first way. It is also judged by the If-none-match and if-modified-since tag values.
The Third way: press CTRL+F5 to force the refresh. At this point, the header information requested by the client is as follows:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/6d/c9/wkiol1vsduyyvwipaage-7n3fs8961.jpg "title=" Clipboard.png "alt=" Wkiol1vsduyyvwipaage-7n3fs8961.jpg "/>
As you can see, there is a cache-control tag with a value of No-cache, which indicates that the client requires the server not to use cached data. So at this point, the server returns the client code must be 200, and returns the new content to the client. At this point, the server-side return HTTP header information is
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/6d/c9/wkiol1vsdwxjick-aahhzfnr9-0908.jpg "title=" Clipboard.png "alt=" Wkiol1vsdwxjick-aahhzfnr9-0908.jpg "/>
As you can see, the response header information still has the associated cache tag.
In httpd.conf, we can set the expiration time of the client cache (that is, set the value of the expires tag)
Expiresactive on #代表开启Expires设置 expiresdefault "Access plus 1 minute" #设置默认的Expires过期时间为1分钟 Expiresbytype text/h tml "Access plus 1 minute" #设置当请求text a resource of type/html, the expiration time is 1 minutes
Understanding the above knowledge, in fact, the configuration is not a problem.
When a client requests a nonexistent Web page or requests an error page, the client and server will not establish the cache. The client cache is introduced here, followed by the server cache.
Server-Side Caching
Server-side caching is built on disk and memory, and it is the time for users to save code logic when they request dynamic pages. At the same time, although the server-side cache files are stored on disk, when the user frequently requests the same page, the server is also directly through the memory to remove the content for users to browse.
We add the following within the httpd.conf configuration file:
Cacheenable disk/cacheroot '/var/apache/cache ' cachedirlevels 2 cachedirlength 1
This configuration is a scenario where we manage caching files on the server.CacheRoot represents the root directory of the cache file, and all cache files will be in this directory.Cachedirlevels is the subdirectory depth in the root directory of the cache file, where 2 representsThere are 2 levels of subdirectories under/var/apache/cache. The cache file is under Level 2 subdirectory.Cachedirlength represents the cache subdirectory name length, here is 1. So our cache subdirectory would have been possible for/var/apache/cache/y/e like this. That/var/apache/cache/y/e This is our cache file. Each one is similar/var/apache/cache/y/e This directory corresponds to a URL of the cache file, the name of the cache file is composed of 22 characters, usually suffix. Data and. Header,.data are stored in the contents of the page, such as "Hello World". Header stores the request header and response header content for this URL. WhenCachedirlength is 1 o'clock, 64 subdirectories will be generated whencachedirlength is 2 o'clock, then each layer will be able to generate 64*64=4096 subdirectories if cachedirlength is also 2, you will be able to generate 64*64*64*64=16777216 subdirectories. So if you do not have so many URLs to be cached, still cachedirlevels and cachedirlength is set to a smaller number to avoid load on disk and I/O.
is the server disk cache file.
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/6d/cd/wkiom1vsdbcsz5dbaadtfqjig10467.jpg "title=" Clipboard.png "alt=" Wkiom1vsdbcsz5dbaadtfqjig10467.jpg "/>
When setting up the disk cache, it is important to note that thecacheroot corresponding directory must already exist and that the HTTPD server has writable permissions.
The cache time of the server is compared by the Max-age value in the Cache-control tag, and the server will not cache content from the cache directory to the client when the client request exceeds the Max-age value in the last server's return header information.
Here, Apache's cache chapter is over. There are other cache-related settings in Apache's configuration, but the main principle is what I said above. The rest depends on people to study slowly. The above is my understanding of the Apache server cache, if there is said wrong place, also welcome correction, common progress.
This article is from the architect's path blog, so be sure to keep this source http://wangweiak47.blog.51cto.com/2337362/1657139
Apache httpd Server Cache Coarse Solution