All Products
Search
Document Center

:Apache cache policy settings

最終更新日:Sep 24, 2021

Disclaimer: this document may contain information about third-party products that are for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the performance and reliability of third-party products, as well as potential impacts of operations on the products.

 

Overview

This topic describes how to use the mod_expires and mod_headers modules of Apache to configure Apache cache policy.

 

Detailed information

mod_expires settings

Apache can Control the Expires and Cache-Control header of HTTP through the mod_expires module of the configuration file. The mod_expires module is used to automatically generate the Expires and Cache-Control tags in the header of the page. This reduces the access frequency and the number of accesses from the client, to reduce unnecessary network traffic and increase the access speed.

 

Introduction to the mod_expires module

The mod_expires command is provided by one of the various Apache modules.

  • ExpiresActive: Enables or disables the generation of Expires and Cache-Control labels.
  • ExpiresByType: sets the cache duration for documents of the MIME type, such as text and html documents.
  • ExpiresDefault: sets the default cache duration for all documents.

The description of expiration time is as follows:

  • access plus 1 month
  • access plus 4 weeks
  • now plus 30 days
  • modification plus 5 hours 3 minutes
  • A2592000
  • M604800

Tips:

  • access plus 1 month, access plus 4 weeks, now plus 30 days and A2592000 are written in the same way, indicating that the expiration time is calculated from the access time.
  • modification plus 5 hours 3 minutes and M604800 have the same meaning, indicating that the validity period starts from the last modification time of the Accessed object.
  • The M604800 only works for static files. The dynamic page generated by the script does not work.

 

Configuration Examples

  1. The configuration of the mod_expires module is as follows:
    • ExpiresActive On: enables the mod_expires function.
    • ExpiresDefault "access plus 6 months": the default cache duration is 6 months.
    • ExpiresByType image/* "access plus 10 years": the cache duration of image files is 10 years.
    • ExpiresByType text/* "access plus 10 years": text type cache duration is 10 years.
    • ExpiresByType application/* "access plus 30 minutes": the cache duration is 30 minutes.
  2. Access the image and jpeg files cached for 315360000 seconds or 10 years.
    缓存时间验证
  3. If you select not cache for image/jpeg and set max-age to 0 seconds, use the following content:
    #ExpiresByType image/* "access plus 10 years"
    ExpiresByType image/* A0
  4. When you access the cached video again, the cache duration is 0 seconds.
    max-age

 

mod_headers module settings

The mod_headers module is configured as follows. For more information, see apache official website.

# YEAR
Header set Cache-Control "max-age=2592000"

# WEEK
Header set Cache-Control "max-age=604800"

# NEVER CACHE
Header set Expires "Thu, 01 Dec 2003 16:00:00 GMT"
Header set Cache-Control "no-store, no-cache, must-revalidate"
Header set Pragma "no-cache"

 

Applicable to

  • CDN