"Web front-end base to combat series of courses"

Source: Internet
Author: User
Tags browser cache web hosting

"Web front-end base to combat series of courses"
  
Absrtact: Mod_expires&mod_headers can reduce the repeat request of about 10%, so that the duplicate user will cache the result of the specified page request and not make a request to the server at all. Before you use it, first make sure that the "Mod_expires" module is enabled. If you install Apache yourself to host web hosting, we can edit Apache's "httpd.conf" profile to handle
  
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 be cached for all files, if we want to set the default cache, choose the way
  
<ifmodule expires_module>
  
Expiresactive on
  
#访问之后的一个月不再更新
  
ExpiresDefault "Access plus 1 month"
  
#访问之后的4周不再更新
  
#ExpiresDefault "Access plus 4 weeks"
  
#访问之后的30天不再更新
  
#ExpiresDefault "Access Plus"
  
</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 directive
  
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 A2592000
  
#脚本文件和css样式, we'd better use modification
  
Expiresbytype Application/javascript M2592000
  
Expiresbytype Text/css M2592000
  
#表示修改后如果没有再次修改, the cache is only allowed to be updated after one weeks
  
Expiresbytype Image/gif M604800
  
Expiresbytype Image/png A604800
  
Expiresbytype Image/jpeg A604800
  
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 years
  
<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 zunyimg888.cn>
  
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>
  
# File cache for Htm,html,txt class one hours
  
<filesmatch "\. (html|www.lbyule.cn htm|txt) $ ">
  
Header set Cache-control "max-age=3600"
  
</filesmatch>
  
# CSS, JS, SWF class file cache for 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 are these issues handled?
  
Cache-control joined www.bjiagw88.cn 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 should also note that http.conf and. Htaccess cannot appear as follows header unset ETag and header unset Last-modifie, otherwise updating the cache becomes a big issue
  
<ifmodule mod_headers>
  
Header unset last-modified
  
Header unset Etag
  
</ifmodule

"Web front-end base to combat series of courses"

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.