The mod_expires module in Apache is used for controlling the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. These headers determine how long a client (browser or proxy) will cache resources. Optimizing these settings can significantly reduce server load and improve client-side experience by reducing load times.
Optimized mod_expires Settings
Here’s an optimized mod_expires section for a production server, covering a variety of file types:
Advertisement
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
# Default expiration: 1 hour after request
ExpiresDefault "now plus 1 hour"
# HTML components
ExpiresByType text/html "access plus 0 seconds"
# Data interchange
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
# Favicon (can be cached for a long time)
ExpiresByType image/x-icon "access plus 1 year"
# Media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
# Webfonts
ExpiresByType font/otf "access plus 1 month"
ExpiresByType font/ttf "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
ExpiresByType font/woff2 "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
# Other
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>
Explanation of Settings
- ExpiresDefault: This is the default caching time for resources that don’t match other rules. It’s set to 1 hour, but you can adjust it based on how often your content changes.
- HTML and Data Interchange: These types of content usually change frequently and are set to expire immediately (0 seconds) or after a short period (1 hour for feeds).
- Favicons: These rarely change and can be cached for a longer period (1 year).
- Media Files: Images, videos, and audios are typically not updated frequently. They are set to expire in 1 month, but this can be increased if these resources rarely change.
- CSS and JavaScript: As these files might change with website updates, but not as frequently as HTML content, they are set to a longer cache period (1 year). Ensure versioning of these files to avoid caching issues when they are updated.
- Webfonts: Fonts generally don’t change once they are set, so a longer caching period (1 month) is appropriate.
- Other Types: For resources like PDFs and specific image types, adjust the caching time based on how often these resources are updated.
Additional Notes
- Adjust these settings based on your specific content update frequency.
- Ensure you have a versioning strategy for assets like CSS and JavaScript to prevent caching issues when these files are updated.
- Keep in mind that aggressive caching might lead to issues when content is updated on the server but still cached in the client’s browser.
- Regularly monitor and adjust these settings based on the specific needs and feedback from the production environment.