This guide covers how to implement effective browser caching for your Trellis-managed WordPress sites. Proper caching can significantly reduce page load times, bandwidth usage, and server load.
- Overview
- Nginx Configuration for Browser Caching
- Implementation
- File Types and Cache Duration
- Testing Cache Implementation
- Troubleshooting
Browser caching instructs visitors' browsers to store static resources locally for a specified period, reducing the need to download the same files on subsequent visits. The included Nginx configuration adds appropriate caching headers to static files such as:
- Images (JPEG, PNG, GIF, WebP, AVIF)
- Stylesheets (CSS)
- JavaScript files
- Fonts (WOFF, WOFF2, TTF, EOT, OTF)
- SVG files
- Other static assets
The nginx-includes/all/assets-expiry.conf.j2 file in this repository contains Nginx configuration to set appropriate caching headers for various file types. The configuration uses the expires directive along with Cache-Control headers to control how long browsers should cache content.
- The configuration maps content types to specific expiration times
- Different file types receive appropriate cache durations
- HTML files are not cached to ensure fresh content
- Admin areas and PHP files are explicitly set to not cache
- Copy the
nginx-includesdirectory to your Trellis project root (same level asserver.yml) - The configuration in
nginx-includes/all/assets-expiry.conf.j2will automatically be applied to all sites on your server - Provision your environment to apply the changes:
# For production
trellis provision --tags nginx-includes production
# For staging
trellis provision --tags nginx-includes staging
# For development
trellis provision --tags nginx-includes developmentNote: Since the file is in the all/ directory, it will be automatically included for every site configured in Trellis. You don't need to modify your wordpress_sites.yml configuration.
The configuration sets the following cache durations:
| File Type | Cache Duration |
|---|---|
| Images (JPEG, PNG, GIF, WebP, AVIF) | 30 days |
| CSS files | 7 days |
| JavaScript files | 7 days |
| Fonts (WOFF, WOFF2, TTF, etc.) | 30 days |
| SVG files | 30 days |
| HTML files | No cache (always fresh) |
| PHP files | No cache |
| Admin areas | No cache |
| Other static files | 1 day |
These durations provide a good balance between performance and freshness. You can adjust them in the assets-expiry.conf.j2 file to match your specific needs.
After implementation, you can verify the caching headers are working correctly:
- Open your website in Chrome or Firefox
- Open Developer Tools (F12)
- Navigate to the Network tab
- Reload the page
- Select any static resource (image, CSS, or JS file)
- Check the Response Headers section for:
Cache-ControlheadersExpiresheaders
curl -I https://example.com/wp-content/uploads/image.jpgLook for headers like:
Cache-Control: public, max-age=2592000
Expires: Wed, 17 Jun 2025 12:00:00 GMT
- Verify the Nginx configuration was properly included
- Check file permissions on the configuration file
- Test with
curl -Ito see the actual headers being sent - Review Nginx error logs:
/var/log/nginx/error.log
If you're developing or frequently updating assets:
- Reduce cache durations in the configuration
- Use versioned file names or query strings (e.g.,
style.css?v=1.2) - Use a cache-busting technique in your build process
For WordPress pages that should always be fresh:
- Verify HTML pages are not being cached (they shouldn't be with this configuration)
- Check for additional caching layers (WordPress plugins, CDN, etc.)
- Clear browser cache during testing
You can modify the cache durations or add more specific rules by editing the nginx-includes/assets-expiry.conf.j2 file. For example, to change the cache duration for images:
# Change image caching from 30 days to 14 days
~image/ 14d;Implementing proper browser caching provides several benefits:
- Reduced bandwidth usage (up to 70% on repeat visits)
- Faster page load times for returning visitors
- Reduced server load and resource consumption
- Improved Google PageSpeed scores
- Better user experience and engagement
For optimal performance, consider combining this caching configuration with image optimization (see the image-optimization directory) and a content delivery network (CDN).