How Apache implements static caching "real-case"

Source: Internet
Author: User
Tags current time browser cache

How Apache implements a static cache can enable Mod_expires&mod_headers


I. Browser caching principle

Remove the "#" font size in front of the line, and then save the "httpd.conf" profile and restart Apache to make the update effective.

Of course, if we are renting a virtual host, "httpd.conf" profile of our general users are not accessible, and in the site root directory to write a ". htaccess" Profile, I think in the use of relatively flexible. " Mod_expires "can also be written in the". htaccess "Profile, in addition to the Apache" httpd.conf "configuration file.

We know that when using a browser to browse the Web page, the browser will store the Web page data cache on the local side, in order to speed up the next time to browse the same page without having to re-download the site, which has accelerated effect. Use the Mod_expires module to speed up Web browsing, where the so-called "acceleration" In fact, the use of "mod_expires" function, to set the expiration time of the webpage file, the length of the page file is saved by the browser cache time. This way, as long as the page file expires, the browser will refer to the cache data, instead of taking the time to download the network Information on the station. On the other hand, the benefit to the webmaster is that it can reduce the traffic consumption of the Web site (for example, some virtual hosts have limited traffic that can be used by the website).

Two. Mod_expires Implement page Caching

LoadModule Expires_module modules/mod_expires.so

Mod_expires default cache directive is ExpiresDefault, this can cache all files, if we want to set the default cache, choose the following way

<ifmodule expires_module>
expiresactive on
#访问之后的一个月不再更新
expiresdefault "Access plus 1 month"
#访问之后的4周不再更新
#ExpiresDefault "Access plus 4 weeks"
#访问之后的30天不再更新
#ExpiresDefault "Access plus days"
</IfModule>


2.1 expiresdefault directive

As described in Apache server, the ExpiresDefault format is as follows

ExpiresDefault "<base> [plus] {<num> <type>}*"
Where the base value is as follows

Access #访问之后, calculated from the current time
Now (equivalent to access) #访问之后, calculated from the current time
Modification #修改之后, calculated from the server file after modification
Plus is the keyword, and this is the system-specific notation.

<num> represents a count in seconds

<type> represents a date unit, the latter value is as follows
Years months weeks days hours minutes seconds 2.2 expiresbytype Instruction

In fact, the ExpiresDefault directive has a very serious caching problem, we should know that all resources are cached, causing the site to update the problem, we should use less expiresdefault and more use of Expiresbytype, Also ExpiresDefault set the time to be as short as possible.

The EXPIRESBYTYPE directive rules are as follows

Expiresbytype type/encoding "<base> [plus] {<num> <type>}*"
Where Base,num,type and ExpiresDefault are similar.
Of course, in Apache Http server, instructions can also be used in shorthand mode

ExpiresDefault type/encoding  [Ufrist (Base)][seconds]
expiresbytype type/encoding   [Ufrist (base)] [ Seconds


Ufrist (Base) indicates the initial capital of base, seconds indicates the expiration time in seconds

<ifmodule expires_module>
  # axxxx-access seconds, indicating how many seconds after
  the visit # mxxxx-modifyed seconds, indicating how many seconds after the modification (recommended)
  expiresactive
  on ExpiresDefault A3600 #表示一小时后更新
  expiresbytype image/x-icon A86400  #表示1天后更新
  #脚本文件和css样式, we'd better use Modification
  expiresbytype application/javascript M604800 #表示修改完之后, updated 1 weeks after update
  Expiresbytype text/css M2592000
  #表示修改后如果没有再次修改, it took one weeks to update the cache 
  expiresbytype image/gif M604800
  expiresbytype image/png A604800
  expiresbytype image/jpeg M604800
  expiresbytype text/plain A604800 expiresbytype
  application/ X-shockwave-flash A604800
  expiresbytype video/x-flv A604800
  expiresbytype application/pdf A604800
  Expiresbytype text/html A900
</IfModule>


In addition, the above wording is good readability, but if it is more convenient, you may try the following wording

<ifmodule expires_module>
expiresactive on
expiresdefault A3600
# 1
<filesmatch "\. ( flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav) $ ">
expiresdefault A9030400
</FilesMatch>
# 1 weeks
<filesmatch "\. (jpg|jpeg|png|gif|swf) $ ">
expiresdefault A604800
</FilesMatch>
# 3 hours
<filesmatch" \ . (TXT|XML|JS|CSS) $ ">
expiresdefault M10800
</FilesMatch>
</IfModule>


three. Mod_headers Cache Implementation

1. About Mod_headers

Apache HTTP Server The description of mod_headers is to customize a request header and response header

2.mod_headers usage

Loading modules

Simple example

<ifmodule headers_module>  
   Header set MyHeader "Hello Joe." It took%d microseconds for Apache to serve this request. "
</IfModule>  
Of course, since we allow customization, we might be able to enhance the cache by adding Cache-control
<ifmodule headers_module>  
# htm,html,txt class file cache for one hours  
<filesmatch "\. ( Html|htm|txt) $ ">  
header Set Cache-control" max-age=3600 "  
</filesmatch>  
  
# css, JS, SWF class file cache one weeks  
<filesmatch "\. (css|js|swf) $ ">  
header Set Cache-control" max-age=604800 "  
</filesmatch>
</IfModule>


3. Resource Update issues

Unlike the Mod_expires module, which has a modification directive, problems arise when resources are updated, and how these problems are handled.

Cache-control joined http/1.1 in order to solve the problem of time accuracy, of course, he has several partners, etag,if-range,last-modified, these several options Apache server itself is implemented, of course, we have to note that Http.conf and. Htaccess can not appear the following header unset ETag and header unset Last-modifie, otherwise the update cache will become a major problem, resulting in Web site update UI failure, data submission error, breakpoint continued failure and other issues.

To solve the above problems, it is best not to appear the following configuration

<ifmodule mod_headers>  
  Header unset last-modified
  header unset Etag
</IfModule>


Note: The ETag may experience a checksum failure when distributed across server requests, resulting in cache effectiveness, which can also cause problems such as update UI failures, data submission errors, and failed breakpoint continuation failures. Therefore, if you are a distributed system, it is recommended that the header unset ETag or Fileetag None.

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.