This topic describes how to configure Apache cache policies by using the mod_expires and mod_headers modules of Apache.
Disclaimer: This topic may contain information about third-party products. The information is only for reference. Alibaba Cloud does not make guarantees or other forms of commitments for the performance and reliability of the third-party tools, or the potential impacts of operations performed by using these tools.
mod_expires
Apache allows you to use the mod_expires module of the configuration file to specify the Expires and Cache-Control headers for HTTP requests. The mod_expires module automatically generates the values of the Expires
and Cache-Control
headers to reduce the frequency and the number of visits from clients. This reduces data transfer and accelerates content delivery.
Description
Among the various modules of Apache, the mod_expires module is easy to configure. The mod_expires module supports the following directives:
ExpiresActive: enables or disables the generation of the Expires and Cache-Control headers.
ExpiresByType: specifies a TTL value for MIME documents, such as text or HTML documents.
ExpiresDefault: specifies a default TTL value for all documents.
Expressions of a TTL value:
access plus 1 month
access plus 4 weeks
now plus 30 days
modification plus 5 hours 3 minutes
A2592000
M604800
access plus 1 month, access plus 4 weeks, now plus 30 days, and A2592000 define TTL values in the same manner. The TTL value starts at the time when the request is received.
modification plus 5 hours 3 minutes and M604800 define TTL values in the same manner. The TTL value starts at the time when the requested file was last modified.
M604800 applies only to static files instead of dynamic pages that are created by using scripts.
Procedure
Enable the mod_expires module. In the Apache configuration file, which is
httpd.conf
or.htaccess
in most cases, add the following directive to enable the mod_expires module:ExpiresActive On
Specify the default TTL value. Use the
ExpiresDefault
directive to specify a default TTL value for all documents. For example, use the following directive to set the TTL value to 6 months:ExpiresDefault "access plus 6 months"
Specify the TTL value based on the MIME type. You can use the
ExpiresByType
directive to specify different TTL values for specific types of resources. Examples:ExpiresByType image/* "access plus 10 years" ExpiresByType text/* "access plus 10 years" ExpiresByType application/* "access plus 30 minutes"
Disable caching for specific types of resources. If you want to disable caching for specific types of resources, such as images, set the max-age parameter to 0 seconds.
ExpiresByType image/* A0
Use the mod_headers module to configure the Cache-Control header. The mod_headers module allows you to configure custom HTTP headers, including Cache-Control. For example, you can allow browsers to always validate cached content:
Header set Cache-Control "no-cache, must-revalidate"
Make sure that you restart Apache after you modify the configuration file for the changes to take effect. The preceding steps describe how to use the mod_expires and mod_headers modules to configure cache policies.
mod_headers
The following example shows how to configure the mod_headers module. For more information, visit the 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"
Restart the service for the changes to take effect
After you modify the configurations, restart the Apache service for the changes to take effect. Run the following command to restart the Apache service:
# (Recommended) Reload the Apache configurations.
sudo systemctl reload apache2
# Or restart the Apache service.
sudo systemctl restart apache2
For OSs that do not use systemctl
:
# (Recommended) Reload the Apache configurations.
sudo service apache2 reload
# Or restart the Apache service.
sudo service apache2 restart