Mod_expires installation and configuration to increase website rate

Source: Internet
Author: User
Tags ranges

Mod_expires installation and configuration to increase website rate

Implementing this method will save you incredible amounts of bandwidth and greatly speed up your site for visitors to your site. Basically, for images, CSS, JavaScript and other files can be optimized for faster downloads, telling your site visitors to cache the memory for them within a certain period of time. The default behavior is to check the last-modified and/or Etag headers of the file each time it is requested.
So a user goes to/home/index.html, and the browser caches all images and files. Then the user leaves the site later, with the browser sending if-modified-since conditional get requests for each cached item, basically look if the files have been changed and they must update their cache.


When you perform the caching method described in this article, you can specify the time that a file or extension is cached for a specific amount. These files are then cached on your site for visitors and they do not send if-modified-since headers until the cache time set has arrived.
#================================================= ============================#
# time CHEAT SHEET
#================================================= ============================#
# 5 M # 604800 1 W
# 2700 M # 1814400 3 W
# 3600 1 H # 2419200 1 M
# 54000 H # 14515200 6 M
# 86400 1 D # 26611200 M
# 518400 6 D # 29030400 1 Y (never expire)

Installing mod_expires.so

1) First find the mod_expires.c file, enter the directory after Apache decompression, execute:

cd/data/software/httpd-2.0.63/

Find. -name MOD_EXPIRES.C

Ls-l MODULES/METADATA/MOD_EXPIRES.C

2) perform apxs-i-a-c/data/software/httpd-2.0.63/modules/metadata/mod_expires.c

3) cd/usr/local/apche2.0.63/modules/will appear mod_expires.so

To this mod_expires installation is complete.

This module controls the expires header content and the max-age instruction of the Cache-control header when the server responds. The validity period (expiration date) can be set relative to the last modification time of the source file or the client's access time.

These HTTP headers show the validity and durability of the document to the client. If there is a cache, the document can be read from the cache (except that it has expired) rather than from the server. The client then examines the copy in the cache to see if it expires or fails to determine whether the update must be obtained from the server.

The simple configuration file is as follows:

Expiresactive on

Expiresbytype image/gif "Access plus month"

Expiresbytype image/jpeg "Access plus month"

# # # Enable Mod_expires

Expiresactive on

# # # set. gif expires 1 months after being visited.

Expiresbytype Image/gif A2592000

# # # Other files are set to expire after 1 days after the last modified time

# # # (with a different syntax)

ExpiresDefault "modification plus 1 day"

# # # Application Cache-control Header properties in index.html file

<files index.html>

Header Append Cache-control "public, must-revalidate"

</Files>

Configuration

The following directives are available:

Expiresactive On|off

ExpiresDefault <code><seconds>

Expiresbytype type/encoding <code><seconds>

1. Where <code> is M or a

M = Expires header shows the file will time out after the modification of this file +<seconds>

A = Expires header shows the file will time out after the access times of this file +<seconds>

2.<seconds> must be an integer

For example, you can set a 1-hour expiration for automatically generated files per hour

ExpiresDefault M3600

For example: set a different expiration time based on type

Expiresbytype text/html A604800 # 1 weeks

Expiresbytype Image/gif A2592000 # one month

Expiresactive on| Off can be set in. htaccess to open for a directory | off.

There is a more humane point of the wording:

ExpiresDefault "<base> [plus] {<num> <type>}*"

Expiresbytypedefault type/encoding "<base> [plus] {<num> <type>}*"

which

1.<base> is:

Access

Now equals access

Modification

2.plus is optional

3.<num> must be an integer

4.<type> is:

Years/months/weeks/days/hours/minutes/seconds

For example, to make a document expire one months after the visit, three ways to do so:

ExpiresDefault "Access plus 1 month"

ExpiresDefault "Access plus 4 weeks"

Expiresdeafult "Access plus days"

Or a more subtle notation:

Expiresbytype text/html "Access plus 1 month days 2 hours"

Expiresbytypes image/gif "modification plus 5 hours 3 minutes"

Implementation method:

1, first of all to determine the configuration a/m or the wording of human nature will also turn into <code><seconds> format. Based on a or m to take base time

1) If the m,base time is r->finfo.mtime (that is, the file modification time), if the file does not exist locally, return (but do not error).

2) If it is a,base time is r->request_time. This time already exists in the request structure;

Note: The r->request_time here is the request moment from the client. This can cause 2 problems:

1) If the client time is inconsistent with the server time, then it is likely that the expires effect we expect will be lost;

2) Each client to the server request files when the request_time are different, then if set a<seconds>, then the client will be on a rolling date, if set m<seconds> Then all the clients are based on the modification time of the server-side files and will expire at the same time (this is especially appropriate for files that are regularly updated, such as news pages), which can cause a huge surge in the expiration time.

2, according to the configuration of the seconds get addtional time;

3, Expiration time expires = base + additional;

4. Generate Cache-control:max-age Header (header),max-age=expires–t->request_time;

PS: Actually, it's a little superfluous here?

expires = base + additional

Base = R->request_time

Max-age = Expires–t->request_time = (base + additional) –r->request_time = Addtional

But this is wrong. Because the expiration time is calculated from R->request_time to expires time ends. If set to M then max-age is not equal to addtional.

For example: The file was modified 3 hours ago to now not move, now set M3600, then Max-age=expires-r->request_time = 7200

In fact, we can see this example:

5. Convert expires time into GMT format, generate expires header, expires value is GMT format of expires time. (Arp_table_setn () function)

When Apache returns to response, we can see that both the Cache-control and the expires headers are set:

Note: The returned headers include their values and we can modify them in the source code (I have modified them here), but it is useless if you do not HTTP1.1 the standard.

Appendix: About HTTP 1.1 Cache

1, when accessing a static file, IE will cache the file locally, if you press ENTER, you will see (cache), indicating that the file is cached locally;

2. If you press F5 (not ctrl+f5), ie re-initiates the request to Web server, and if it does not expire, Web server returns 304:

"Get/index.html http/1.1″304–"-"

Apache will have this log. Description Apache accepted the request, but did not transmit any bytes except for the head:

Here's the last field:%b = the number of bytes transmitted in CLF format except for the HTTP header, which means '-' instead of 0 when there is no byte transfer

If you want to reduce the pressure on the backend server, do not backed server to any useless requests (304), then set up squid server in backend server front end, Returned by Squid 304. A large number of 304 requests are generated without the backend server being validated.

3, Ctrl+f5 is equivalent to a new request, return code 200.

PostScript: Abolition of Mod_expires?

We see Mod_expires is nothing more than for us to add 2 http Response Header,apache inside already have the Header command:

Header Add "Expires" "Mon, Jul 2099 23:30:00 GMT"

Header Add "Cache-control" "max-age=31536000″# 1 years

Header Add "Cache-control" "Post-check=31536000″

Header Add "Cache-control" "Pre-check=31536000″

This is a completely alternative to MOD_EXPIRES.C.

Note:

1. Whether or not there is mod_expires, with IE to access, there will be a cache, and with the mod_expires effect, such as the installation of Mod_expires necessary???

2. Increase the difference before and after Mod_expires: After installing HttpWatch, look at the header of the request, found that after installing the MoD add expires and Cache-control,

#================================================= ============================#
# time CHEAT SHEET
#================================================= ============================#
# 5 M # 604800 1 W
# 2700 M # 1814400 3 W
# 3600 1 H # 2419200 1 M
# 54000 H # 14515200 6 M
# 86400 1 D # 26611200 M
# 518400 6 D # 29030400 1 Y (never expire)

The main function of the Mod_expires module is to automatically generate expires tags and cache-control tags in the header information of the page, thus reducing the frequency and number of visits to the client, reducing unnecessary traffic and increasing access speed.

Mod_expires is one of the more simple configurations in Apache's many modules, with only three instructions:

Expiresactive
Expiresbytype
ExpiresDefault
expiresactive directive: Turns on or off the ability to produce "Expires:" and "Cache-control:" headers.

EXPIRESBYTYPE directive: Specifies the expiration time of the MIME-type document (for example: text/html).
ExpiresDefault directive: The default time for all documents to expire.

How the Expiration Time is written:

"Access plus 1 month"
"Access plus 4 weeks"
"Now plus"
"Modification plus 5 hours 3 minutes"
A2592000
M604800
Access, now, and a three are written in the same way, meaning that the expiration time is calculated from the point of access.
The meaning of modification and M is the same, which means that the expiration time is calculated based on the last modification time of the file being accessed.
Therefore, the latter method only works on static files, and the dynamic pages generated by the script are not affected by it.
As follows:
The first solution is Apache Module Mod_expires 1.3 2.0 2.2
Expiresactive on
ExpiresDefault A300
Expiresbytype Image/x-icon A2592000
Expiresbytype Application/x-javascript A2592000
Expiresbytype Text/css A2592000
Expiresbytype Image/gif A604800
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 A300
A second solution is Mod_headers 1.3 2.0 2.2
# year
<filesmatch "\. (Flv|gif|ico) $ ">
Header set Cache-control "Max-age=2592000″
</FilesMatch>
# WEEK
<filesmatch "\. (PDF|SWF|JS|CSS) $ ">
Header set Cache-control "Max-age=604800″
</FilesMatch>
# never CACHE
<filesmatch "\. (html|cgi|php|htm) $ ">
Header set Expires "Thu, DEC 2003 16:00:00 GMT"
Header set Cache-control "No-store, No-cache, Must-revalidate"
Header set Pragma "No-cache"
</FilesMatch>
Note: Use FilesMatch and files in the htaccess file
This is the moment when headers is loading a JPEG image,
This caching scheme is implemented and does not have a cache effect.
When a JPEG is not cached
Last-modified:wed, 2006 12:16:56 GMT
ETag: "B57d54-45e7″
Accept-ranges:bytes
content-length:17895
Connection:close
Content-type:image/jpeg
Cache-Over
cache-control:max-age=2592000
Expires:tue, Mar 2006 16:23:52 GMT
Last-modified:wed, 2006 12:16:56 GMT
ETag: "B57d54″
Accept-ranges:bytes
content-length:17895
Connection:close
Content-type:image/jpeg
Content-language:en
Report:
Apache Configuration File Example:
Example 1
# htm files are php
AddHandler application/x-httpd-php. php. htm
# setup errordocuments to local PHP file
ErrorDocument 404/cgi-bin/error.htm
ErrorDocument 403/cgi-bin/error.htm
ErrorDocument 500/cgi-bin/error.htm
# Turn on Expires and set default Expires-3 days
Expiresactive on
ExpiresDefault A259200
# Set up caching on media files for 1 month
<filesmatch "\. (Ico|gif|jpg|jpeg|png|flv|pdf|swf|mov|mp3|wmv|pp t) $ ">
ExpiresDefault A2419200
Header Append Cache-control "public"
</FilesMatch>
# Set up 2 Hour caching on commonly updated files
<filesmatch "\. (XML|TXT|HTML|JS|CSS) $ ">
ExpiresDefault A7200
Header Append Cache-control "Private, Must-revalidate"
</FilesMatch>
# force no caching for dynamic files
<filesmatch "\. (php|cgi|pl|htm) $ ">
ExpiresDefault A0
Header set Cache-control "No-store, No-cache, Must-revalidate, Max-age=0″
Header set Pragma "No-cache"
</FilesMatch>
Example 2
# htm files are php
AddHandler application/x-httpd-php. php. htm
# setup errordocuments to local PHP file
ErrorDocument 404/cgi-bin/error.htm
ErrorDocument 403/cgi-bin/error.htm
ErrorDocument 500/cgi-bin/error.htm
# Turn on Expires and set default to 0
Expiresactive on
ExpiresDefault A0
# Set up caching in media files for 1 year (forever?)
<filesmatch "\. (ico|flv|pdf|mov|mp3|wmv|ppt) $ ">
ExpiresDefault A29030400
Header Append Cache-control "public"
</FilesMatch>
# Set up caching in media files for 1 week
<filesmatch "\. (gif|jpg|jpeg|png|swf) $ ">
ExpiresDefault A604800
Header Append Cache-control "public, proxy-revalidate"
</FilesMatch>
# Set up 2 Hour caching on commonly updated files
<filesmatch "\. (XML|TXT|HTML|JS|CSS) $ ">
ExpiresDefault A7200
Header Append Cache-control "Private, Proxy-revalidate, Must-revalidate"
</FilesMatch>
# force no caching for dynamic files
<filesmatch "\. (php|cgi|pl|htm) $ ">
ExpiresDefault A0
Header set Cache-control "No-cache, No-store, Must-revalidate, max-age=0, Proxy-revalidate, No-transform"
Header set Pragma "No-cache"
</FilesMatch>

It is necessary to remind you that the URL address, which is processed by expires, can be cached by the browser and proxy server, and should only be used where needed.

Mod_expires installation and configuration to increase website rate

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.