Reduces server load. Apache enables the mod_expires module and apachemod_expires

Source: Internet
Author: User

Reduces server load. Apache enables the mod_expires module and apachemod_expires

Mod_expires can reduce repeated requests by about 10%, so that duplicate users CACHE the results of specified page requests locally and do not send requests to the server at all.

Before use, check whether the "mod_expires" module is enabled. if you install Apache to set up a web host, you can edit httpd in Apache. conf "configuration file to process. search, you may find this line:

Copy code

Delete the "#" font size before the row, store the "httpd. conf" configuration file, and restart Apache to make the update take effect.

Of course, if we are renting a virtual host, "httpd. conf "configuration file we generally do not have access to, but write it in the root directory of the website ". htaccess "configuration file, I think it is relatively flexible to use. the configuration materials of "mod_expires" can be written in Apache "httpd. conf "can also be written in ". in the htaccess configuration file.

We know that when we use a browser to browse a webpage, the browser will Cache the webpage data and store it on the local client to speed up next browsing of the same webpage without having to download it from the website again, acceleration Effect. the mod_expires module is used to accelerate web page browsing. The so-called "acceleration" actually uses the "mod_expires" function to set the expiration time of web page files, extend the time when a webpage file is cached by the browser. as a result, as long as the expiration time of the webpage file is not reached, the browser will reference the cached data, instead of spending time downloading the data on the website. on the other hand, the benefits of webmasters are that they can reduce the traffic consumption on websites by PAGE visitors (for example, some virtual hosts limit the traffic available for websites ).

Next, let's learn from the example.
Example 1:

Copy code

Example 2:

Copy code

Example 3:

Copy code

Use the <IfModule> </IfModule> package folder command to avoid execution if the mod_expires module is not enabled. if the mod_expires module is enabled, disable <IfModule> </IfModule> or disable it.

ExpiresActive On indicates that the mod_expires function is enabled, and the Off function is disabled.

The ExpiresDefault command sets the preset expiration time.
From Example 1 and example 2, you can see that there are two ways to set the time: one is the text narration type, and the other is the code plus the number of seconds type.
Text narration type:
"Access plus 10 days" means 10 days from the browsing time. according to the Apache official documents, there are three expiration start times: access, now, and modification. access and now have the same meaning, while modification refers to the "last editing time" of the webpage file ". therefore, if you want to start with the last editing time of, you can write it like this, "modification plus 10 days ". the time is also very simple, that is, the English word (years, months, weeks, days, hours, minutes, seconds ). for example, you can write it as follows, "access plus 1 month 15 days 2 hours ".

Number of seconds added to the Code: 
A86400 indicates that the browsing time starts from 1 day. the format is code plus seconds. there are two types of Code: "A" is equivalent to "access", which means the expiration time is calculated from the browsing time. using Code "A" is more suitable for applications that do not change frequently on web page file types, such as examples. the other code is "M", which is equivalent to "modification", which refers to the "last editing time" of the webpage file ". the "M" code is more suitable for applications that frequently change the type of webpage files, such as HTML pages that frequently update content. in seconds, I will attach references to the post for your quick reference.

The ExpiresByType command sets the expiration time based on different webpage file types.
For example, ExpiresByType text/css A2592000 indicates that the CSS style file on the website expires three days later; ExpiresByType image/gif A604800 indicates that the Gif image on the website expires seven days later.

In Example 3, <FilesMatch> </FilesMatch> is used to folder webpage files of different types, instead of using the "ExpiresByType" command. This is also a usage.

 

 

Use the mod_expires and mod_headers modules of Apache to implement file caching. Add an Expires header | specify Expires for the file header

Use the mod_expires and mod_headers modules of Apache to implement file caching. Add an Expires header | specify Expires for the file header

When you use YSlow to optimize the website speed, you will often see that the value of Add an Expires header is very low. There are a lot of searches, but you still don't know how to do it. Below is the answer.

Add an Expires header/specify Expires for the file header
Add an expiration sign to the static file. Cache the browser or CDN server to accelerate the loading of images and other static files.
Expires is part of the browser Cache mechanism. The browser Cache depends on four values in the Header: Cache-Control, Expires, Last-Modified, and ETag.
To optimize this option, you must set Cache-Control and Expires for all files on the site.

The apache module mod_expires and mod_headers can be used to add the expiration mark.

By configuring the. htaccess file, you can easily set the cache time by file type. It helps to speed up your website.

1. Use mod_expires
Add the following statement to. htaccess:
<Ifmodule mod_expires.c>
Expiresactive on

# The default cache time for all files is set to 300 seconds.
Expiresdefault a300

# Html, plain-text cache: 300 seconds
Expiresbytype text/html a300
Expiresbytype text/plain a300

# Css, javascript cache for one hour
Expiresbytype text/css a3600
Expiresbytype application/x-javascript a3600

# Icon File Cache for 30 days
Expiresbytype image/x-icon a2592000

# Image class cache for one week
Expiresbytype image/jpeg a604800
Expiresbytype image/gif a604800
Expiresbytype image/png a604800

# Cache other files for one week
Expiresbytype application/x-shockwave-flash a604800
Expiresbytype video/x-flv a604800
Expiresbytype application/pdf a604800

</Ifmodule>

But one problem is that our common Apache host often does not support mod_expires. It doesn't matter. We use mod_headers in another module.

You can also add the following content to the. htaccess file to implement Caching:

<Ifmodule mod_headers.c>

# Htm, html, and txt files are cached for an hour.
<Filesmatch "\. (html | htm | txt) $">

Header set cache-control "max-age = 3600 ″
</Filesmatch>

# Cache css, js, and swf files for one week
<Filesmatch "\. (css | js | swf) $">
Header set cache-control "max-age = 604800 ″
</Filesmatch>
# Cache jpg, gif, jpeg, png, ico, flv, pdf, and other files for one year
<Filesmatch "\. (ico | gif | jpg | jpeg | png | flv | pdf) $">
Header set cache-control "max-age = 29030400 ″
</Filesmatch>

</Ifmodule>

Sample Code:

<FilesMatch "\. (ico | pdf | flv | jpg | jpeg | png | gif | js | css | swf) $">
Header set Cache-Control "max-age = 604800, public"
</FilesMatch>
<FilesMatch "\. (xml | txt) $">
Header set Cache-Control "max-age = 18000, public, must-revalidate"
</FilesMatch>
<FilesMatch "\. (html | htm | php) $">
Header set Cache-Control "max-age = 3600, must-revalidate"
</FilesMatch>

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.