Home php教程 PHP开发 Apache enables mod_expires module

Apache enables mod_expires module

Dec 21, 2016 am 11:36 AM

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the apache version How to view the apache version Apr 13, 2025 pm 01:00 PM

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

See all articles