magento2: Varnish cache not working
Steps to reproduce
- Install Magento 2.0.7 on Centos with Nginx, PHP 7.07 FPM and MariaDB 10.1
set $MAGE_MODE production;is set in Nginx server block- Some content is added
- Varnish 4.03 is installed and configured. Note that Varnish was installed after Magento2 installation. All of that is currently running on the same server.
- Magento is configured to use Varnish Caching
varnish.vclgenerated by “Export VCL for Varnish 4” is being used- We have around 46k products imported - none of them are enabled yet however
Expected result
- Pages are cached with ~100ms response time
Actual result
- Pages are not cached - it actually takes ~3 seconds to load
- Static files (CSS/JS) are cached
- Sysadmin is saying that PHP (Magento) is sending “no cache” header and therefore Varnish ignores the request.
- Following response headers are present:
Via:1.1 varnish-v4
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Magento-Cache-Debug:MISS
X-Powered-By:PHP/7.0.7
X-Varnish:262271
X-XSS-Protection:1; mode=block
What could be the issue?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (6 by maintainers)
After spending hours trying to figure out why Magento was sending no-store I came up with this:
Search code folder for disabled cache tag
grep -rnw ‘/var/www/magento/app/code/’ -e ‘cacheable=“false”’
This will output all files that have a cacheable=“false”. This makes it easy to identify which extension is the culprit.
Wanted to chime in here and say we were experiencing the exact symptoms and in our case it was a third party module.
Specifically this one: https://github.com/YotpoLtd/magento2-plugin
Long story short this line was the issue. When cacheable is set to false Magento sent
X-Magento-Cache-Control: no-store, no-cache, must-revalidate, max-age=0preventing Varnish from caching the response.When cacheable is set to true (or if we disabled the module completely) Magento sent
X-Magento-Cache-Control:max-age=86400, public, s-maxage=86400and Varnish correctly cached the response.Your mileage may vary, but these issues sounded close enough that I figure I’d offer up our findings. @rgoncharuk let me know if this sounds like a separate issue and I’ll open up a ticket.