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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

PHP Framework Performance Comparison: The Ultimate Showdown of Speed ​​vs. Efficiency PHP Framework Performance Comparison: The Ultimate Showdown of Speed ​​vs. Efficiency Apr 30, 2024 pm 12:27 PM

According to benchmarks, Laravel excels in page loading speed and database queries, while CodeIgniter excels in data processing. When choosing a PHP framework, you should consider application size, traffic patterns, and development team skills.

Integration and expansion of golang function concurrency control and third-party libraries Integration and expansion of golang function concurrency control and third-party libraries Apr 25, 2024 am 09:27 AM

Concurrent programming is implemented in Go through Goroutine and concurrency control tools (such as WaitGroup, Mutex), and third-party libraries (such as sync.Pool, sync.semaphore, queue) can be used to extend its functions. These libraries optimize concurrent operations such as task management, resource access restrictions, and code efficiency improvements. An example of using the queue library to process tasks shows the application of third-party libraries in actual concurrency scenarios.

How to conduct concurrency testing and debugging in Java concurrent programming? How to conduct concurrency testing and debugging in Java concurrent programming? May 09, 2024 am 09:33 AM

Concurrency testing and debugging Concurrency testing and debugging in Java concurrent programming are crucial and the following techniques are available: Concurrency testing: Unit testing: Isolate and test a single concurrent task. Integration testing: testing the interaction between multiple concurrent tasks. Load testing: Evaluate an application's performance and scalability under heavy load. Concurrency Debugging: Breakpoints: Pause thread execution and inspect variables or execute code. Logging: Record thread events and status. Stack trace: Identify the source of the exception. Visualization tools: Monitor thread activity and resource usage.

The evasive module protects your website from application layer DOS attacks The evasive module protects your website from application layer DOS attacks Apr 30, 2024 pm 05:34 PM

There are a variety of attack methods that can take a website offline, and the more complex methods involve technical knowledge of databases and programming. A simpler method is called a "DenialOfService" (DOS) attack. The name of this attack method comes from its intention: to cause normal service requests from ordinary customers or website visitors to be denied. Generally speaking, there are two forms of DOS attacks: the third and fourth layers of the OSI model, that is, the network layer attack. The seventh layer of the OSI model, that is, the application layer attack. The first type of DOS attack - the network layer, occurs when a large number of of junk traffic flows to the web server. When spam traffic exceeds the network's ability to handle it, the website goes down. The second type of DOS attack is at the application layer and uses combined

How to add a server in eclipse How to add a server in eclipse May 05, 2024 pm 07:27 PM

To add a server to Eclipse, follow these steps: Create a server runtime environment Configure the server Create a server instance Select the server runtime environment Configure the server instance Start the server deployment project

Application of algorithms in the construction of 58 portrait platform Application of algorithms in the construction of 58 portrait platform May 09, 2024 am 09:01 AM

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

How to deploy and maintain a website using PHP How to deploy and maintain a website using PHP May 03, 2024 am 08:54 AM

To successfully deploy and maintain a PHP website, you need to perform the following steps: Select a web server (such as Apache or Nginx) Install PHP Create a database and connect PHP Upload code to the server Set up domain name and DNS Monitoring website maintenance steps include updating PHP and web servers, and backing up the website , monitor error logs and update content.

Java package management and integration of dependencies and version control Java package management and integration of dependencies and version control Apr 24, 2024 pm 09:48 PM

In Java, package management and version control integration are crucial, using Maven to manage dependencies and Git for version control. The integration steps include initializing the Git repository, creating the Maven function package information file, and adding it to the Git repository. In a practical case, add the CommonsLang dependency, use Maven to download it and add it to the Git repository to ensure that the team uses the same dependency version.

See all articles