How to Configure Apache mod_expires for Caching
mod_expires sets cache expiry headers that tell browsers how long to keep static resources locally. Proper Caching reduces server load and improves page load times. This guide walks through the specific troubleshooting steps to diagnose and resolve Caching issues.
Before You Begin
Before you begin, be sure to have the following in place:
- A Linux server with the relevant software installed
- Access to the command line interface
- Appropriate permissions (root or sudo)
Quick Fix
Wrong
No expiration headers set (browsers re-request every resource)
Wrong: No cache control headers
Right
ExpiresActive On\nExpiresByType image/png "access plus 1 year"\nExpiresByType text/css "access plus 1 month"\nExpiresByType application/javascript "access plus 1 month"\nExpiresDefault "access plus 1 day"
Right: Setting Expires headers per MIME type
Output
mod_expires configured:\n images: 1 year\n CSS/JS: 1 month\n Default: 1 day
Prevention
To avoid future issues, follow these best practices:
- Enable mod_expires with a2enmod expires
- Set long cache times for versioned static assets
- Use shorter cache times for non-versioned resources
- Combine with mod_headers for Cache-Control fine-tuning
- Test with browser developer tools to verify cache headers
DodaTech Tools
For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.
Common Mistakes with mod expires
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world APACHE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro