Solving Magento’s No-Cache Headers for Varnish Caching
Varnish is an excellent tool for caching Magento websites to improve performance. However, one of the most common issues developers face is Magento sending no-cache headers, preventing Varnish from storing cached responses.
Understanding Magento’s No-Cache Headers
By default, Magento sends a Cache-Control: no-cache header and sets the Expires header to “0”. This is because Magento needs to frequently update data like prices, stock levels, or product availability. For static content that doesn’t change often, this behavior can be quite problematic.
Why Varnish Needs Cache Headers
Varnish uses these headers to determine whether a response should be stored in cache. If the Cache-Control header includes no-cache, Varnish won’t store the response even if it could otherwise be cached.
Solving the Issue: Turning Off No-Cache Parameters
To solve this, we need to modify Magento’s behavior so that it stops sending no-cache headers for static content. Here are two common approaches:
1. Configuring Magento’s Cache Manager
Magento provides a built-in cache manager that allows you to enable caching for various areas of your site. To configure this:
- Navigate to the Magento admin panel.
- Go to Stores > Configuration > Advanced > System.
- Under “Cache Management,” set “Caching Allowed” to Yes.
This won’t completely eliminate no-cache headers, but it will allow Magento to use its built-in cache for some data, reducing the number of uncachable responses sent to Varnish.
2. Using NGINX to Remove No-Cache Headers
Since you’re using NGINX, a more robust solution is to configure your web server to remove no-cache headers from Magento’s responses before they reach Varnish.
In your NGINX configuration (usually located in the sites-available directory), add the following directives within the server block for your Magento site:
location ~* \.(js|css|png|jpg|jpeg|gif)$ {
expires 365d;
add_header Cache-Control "public, must-revalidate";
}
This configuration tells NGINX to cache all static files (JavaScript, CSS, images) for a year and to set the Cache-Control header accordingly. When these requests reach Varnish, it will interpret this as a directive to cache the response.
Additional Best Practices
- Make sure your Magento site is properly configured for caching. In the admin panel, go to System > Configuration > Advanced > Developer and ensure that both “Enable JavaScript Merge” and “Enable CSS Merge” are set to Yes.
- Consider using Magento’s built-in Varnish configuration tool or a third-party extension like Varnish Cache Extensions for more advanced caching options.
Conclusion
While Magento has its reasons for sending no-cache headers, they can cause serious performance issues when using Varnish. By configuring either Magento’s cache manager or your NGINX setup, you can effectively enable Varnish to cache most of your site’s content.
Remember, it’s crucial to test your caching setup thoroughly after making changes. In some cases, you may need to tweak the configuration further based on your specific requirements and traffic patterns.
For more detailed guides on Magento optimization and Varnish integration, feel free to explore our Magento Performance Series at LaravelCompany.