Home  >  Article  >  php教程  >  Apache enables mod_expires module

Apache enables mod_expires module

黄舟
黄舟Original
2016-12-21 11:36:131263browse

mod_expires can reduce repeated requests by about 10%, allowing repeated users to cache the results of specified page requests locally without making requests to the server at all.

Before using it, first make sure whether the "mod_expires" module is enabled. If you install Apache yourself to set up a web host, here we can handle it by editing Apache's "httpd.conf" configuration file. Search After a while, you may find a line like this:

#LoadModule expires_module modules/mod_expires.so

Copy the code

Delete the "#" character in front of the line, then save the "httpd.conf" configuration file and restart it Start Apache to make this update take effect.

Of course, if we are renting a virtual host, the "httpd.conf" configuration file is not accessible to ordinary users, and we write a ".htaccess" setting in the root directory of the website file, I think it is relatively flexible in use. In addition to being written in Apache's "httpd.conf" configuration file, the "mod_expires" setting data can also be written in the ".htaccess" configuration file.

We know that when using a browser to browse a web page, the browser will cache the web page data and store it on the local machine to speed up the next time you browse the same web page without having to download it from the website again, thereby speeding up the process. Effect. Use the mod_expires module to speed up web browsing. The so-called "acceleration" here is actually using the "mod_expires" function to set the expiration time of web page files and lengthen the time that web page files are saved in the browser cache (Cache). In this way, as long as the expiration time of the web page file has not expired, the browser will refer to the cached data without having to spend time downloading the data on the website. On the other hand, the benefit to the webmaster is that it can reduce browsing time The traffic consumption of the website (for example, some virtual hosts limit the traffic that the website can use).

Let’s learn directly from the examples.
Example 1:

ExpiresActive On

ExpiresDefault “access plus 10 days”

ExpiresByType text/css “access plus 1 second”

Copy code

Example 2:

ExpiresActive On

ExpiresDefault A86400

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

Expi resByType text/html A900

Copy code

Example 3:

ExpiresActive On

ExpiresDefault A0

# 1 year

ExpiresDefault A9030400

# 1 week

ExpiresDefault A604800

# 3 hours

ExpiresDefault A10800″

Copy code

Using to wrap instructions can avoid having to execute them when the mod_expires module is not enabled. If the mod_expires module is determined to be enabled, then It doesn’t matter if you don’t write .

ExpiresActive On means to enable the mod_expires function, and Off means to turn off the function.

ExpiresDefault command is to set the default expiration time.
From Example 1 and In Example 2, you can see that there are two ways to set time, one is text description type, and the other is code plus seconds type.
Text description type:
"access plus 10 days" means browsing time The starting time is 10 days. According to the official Apache documentation, there are three starting times for expiration, namely access, now and modification. Access and now have the same meaning, and modification refers to the "last editing time" of the web page file. So if you want to use the file Calculated from the last editing time, it can be written like this, "modification plus 10 days". The time specification is also very simple, which is English words (years, months, weeks, days, hours, minutes, seconds). For example, it can be written like this , "access plus 1 month 15 days 2 hours".

Code plus seconds type:
A86400 means 1 day from the time of browsing. The format is code plus seconds. There are two types of codes, "A" is equivalent to" access", which means the expiration time is calculated from the time of browsing. The code "A" is more suitable for web file types that do not change frequently, such as images. The other code is "M", which means the same as "modification", which refers to It is the "last editing time" of the web page file. Using the code "M" is more suitable for applications in web page file types that change frequently, such as HTML pages that frequently update content. I have attached reference materials at the end of the article for the seconds information. For your quick reference.

The ExpiresByType command sets the expiration time according to different web page file types.
For example, ExpiresByType text/css A2592000 means that the CSS style file on the website will expire in 3 days; ExpiresByType image/gif A604800 means that the CSS style file on the website will expire in 3 days. Gif files expire after 7 days.

In Example 3, is used to include various types of web files instead of using the "ExpiresByType" command. This is also a usage.


Use the Apache module mod_expires and mod_headers to implement file caching, Add an Expires header|Specify Expires for the file header

Use the Apache module mod_expires and mod_headers to implement file caching, Add an Expires header|Specify Expires for the file header

When you are using YSlow’s website speed optimization, you often see that the Add an Expires header has a very low score, and you search a lot but don’t know what to do. Here's the answer.

Add an Expires header / Specify Expires for the file header
Add an expiration mark to the static file. Let the browser or CDN server cache it to speed up the loading of images and other static files.
Expires is part of the browser Cache mechanism. The browser's cache depends on four values ​​​​in the Header: Cache-Control, Expires, Last-Modified, ETag.
To optimize this option, all you need to do is to set Cache-Control and Expires for all files in the site.

To add expiration flags, we can use the apache modules mod_expires and mod_headers.

By configuring the .htaccess file, you can easily set the cache time by file category. It is helpful to improve website speed.

1. Use mod_expires
to add the following statement in .htaccess:

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 is cached for one week
expiresbytype image/jpeg a604800
expiresbytype image/gif a604800
expiresbytype image/png a604800

#Other files are cached for one week
expiresbytype application/x-shockwave-flash a6048 00
expiresbytype video /x-flv a604800
expiresbytype application/pdf a604800

But one problem is that our commonly used Apache hosts often don’t support mod_expires. It doesn’t matter, we use another module to use mod_headers.

Also add the following content to the .htaccess file to achieve caching:

# Files such as htm, html, txt are cached for one hour

header set cache-control “max-age=3600″

# css, js, swf files are cached for one week

header set cache-control “max-age=604800″

# jpg, gif, jpeg, png, ico, flv, pdf and other files are cached for one year

header set cache-control “max-age=29030400″

The following are Sample code:


Header set Cache-Control “max-age=604800, public”


Header set Cache-Control “max-age=18000, public, must-revalidate”


Header set Cache-Control “max-age=3600, must-revalidate”

The above is the content of Apache enabling mod_expires module, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn