How to Set cache validity period in Apache

Source: Internet
Author: User

How to Set cache validity period in Apache

Today, I learned how to set cache time in Apache and remember it as a memo.

In the http header, the two fields related to the Cache time are Expires and max-age in Cache-Control. Expires sets a Cache expiration time, max-age sets a cache validity period (in seconds). Normally, the two can push each other. (For details, refer to the browser cache mechanism analysis)

For example, WampServer. Create an index.html page in the www folder and open it in the browser. The http response header is as follows:

Obviously, the Expires or max-age words are not displayed.

For example, if I want to set max-age to 1000, how?

Method 1: Start and configure Expires

Open the httpd. conf file (for example, under F: \ wamp \ bin \ apache \ Apache2.2.17 \ conf) and find this line:

#LoadModule expires_module modules/mod_expires.so

Remove the comments (well number), add the two lines, and restart apache, then OK:

<IfModule mod_expires.c>    ExpiresActive On    ExpiresByType text/html A1000</IfModule>

We can see that the max-age field also appears in the Response Header. In fact, it is automatically filled according to the Expires settings.

Other settings are similar:

# Enable expires_module LoadModule expires_module modules/mod_expires.so # enable validity period control ExpiresActive On # GIF is valid for 1 month ExpiresByType image/gif A2592000 # HTML document is valid for one week after the last modification time expiresByType text/html M604800 # the following meanings are similar to ExpiresByType text/css "now plus 2 months" ExpiresByType text/js "now plus 2 days" ExpiresByType image/jpeg "access plus 2 months" expiresByType image/bmp "access plus 2 months" ExpiresByType image/x-icon "access plus 2 months" ExpiresByType image/png "access plus 2 months"
Method 2: Modify cache-control using the mod_headers Module

You can also directly modify the max-age field.

Find the line in the httpd. conf file:

#LoadModule headers_module modules/mod_headers.so

Remove the note (well number) and add this sentence. Restart apache and you will be OK:

header set cache-control "max-age=1000"

We found that the response header does not contain the Expires field.

Install a Web Server on Ubuntu Server 14.04 (Linux + Apache + MySQL + PHP)

Install and configure the PHP environment in Linux (Apache2)

Install the LAMP \ Vsftpd \ Webmin \ phpMyAdmin service and settings in Ubuntu 13.04

Compile and install LAMP in CentOS 5.9 (Apache 2.2.44 + MySQL 5.6.10 + PHP 5.4.12)

Source code for Web server architecture in RedHat 5.4 build the LAMP environment and application PHPWind

Apache details: click here
Apache: click here

This article permanently updates the link address:

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.