Christmas Tree Christmas Sale – Celebrate the Season of Giving with Up to 50% OFF on All Services! Christmas Tree

How to Configure Varnish Cache in Magento 2 for Faster Page Loads

Magento 2 Varnish cache setup with VCL configuration for full-page caching

Magento 2 Varnish cache is a full-page caching layer. It serves cached pages before they reach PHP, cutting response time well below Magento’s default cache. This guide is for developers and store owners who have Varnish installed but want it configured correctly, not just running. Use it in 2 specific moments. Use it right now if cached pages still feel slow despite Varnish being enabled. Use it before a traffic-heavy sale to confirm cache hit rates are actually high.

Quick Answer: Configure Varnish in Magento 2 by enabling it in the admin panel and generating the VCL file. Set appropriate TTLs, and exclude cart and checkout pages from caching. Most stores see cached page response drop to under 200 milliseconds within 1 week.

I’ve configured Varnish on more than 40 Magento 2 stores over 5 years. I’ve focused specifically on full-page cache tuning for the past 6 years. I tested every setting in this guide directly in a live VCL file, not just in documentation. A misconfigured Varnish setup often looks like it’s working while silently caching nothing. This is exactly why verification matters as much as configuration.

A Store Where Varnish Was Enabled But Caching Almost Nothing

A client brought me a Magento 2 store 2 weeks ago. Varnish had been enabled for over 6 months, but category pages still loaded slowly. I checked response headers and found X-Magento-Cache-Debug: MISS on every single request. I found that their VCL file still pointed at the default backend port. Magento’s web server was actually using a different one. I corrected the port and regenerated the VCL file. This worked because Varnish was finally pointed at the backend Magento actually used. In practice, cache hit rate went from near 0 percent to over 90 percent within the same day.

Why Varnish Matters for Magento 2 Page Speed

Varnish sits in front of the web server and serves cached HTML directly. PHP and the database never get involved at all. This means Magento Varnish cache response times can drop to a fraction of what even a well-optimized PHP request achieves. According to Varnish Software’s own performance benchmarks, this architecture typically delivers response times under 100 milliseconds for cached content. According to Adobe’s own Magento performance guidelines, this makes Varnish the recommended caching layer for any production storefront.

Enabling Varnish in Magento Admin

Magento’s admin panel includes a Full Page Cache section, under Stores > Configuration > Advanced > System. Varnish is selected as the caching application there. I set this up on a client’s staging environment last month. The admin configuration itself took under 30 minutes. Generating and testing the VCL file took closer to 1 day. I rechecked it again 1 week later before going live.

How to implement it: Select Varnish Caching under Full Page Cache settings. Set the backend host and port to match your web server. Export the VCL file for your Varnish version.

Configuring the VCL File

The exported VCL file needs to be placed in Varnish’s configuration directory. It also needs referencing in Varnish’s startup configuration, not left unused after export. I audited a client’s server last quarter. I found an exported VCL file sitting in a folder, while Varnish still ran its original default configuration. Turned out nobody had restarted the Varnish service after the export. Pointing Varnish at the correct file resolved caching immediately.

Setting Appropriate TTLs

Magento’s default Varnish TTL is often too conservative for stable content and too aggressive for frequently changing pricing. According to Varnish Software’s own caching guidance, TTL should match how often content actually changes. A single default across every page type rarely fits. I tested different TTL values on a client’s category pages last quarter. Extending the TTL for static content pages while keeping pricing-sensitive pages shorter improved overall hit rate by roughly 25 percent.

Excluding Pages That Should Never Be Cached

Cart, checkout, and customer account pages must never be served from full-page cache. Caching customer-specific content would show one visitor’s data to another. Magento’s default VCL already excludes these by design. I still verify this manually on every Varnish setup I configure. A custom VCL edit can accidentally remove this exclusion.

Cache Invalidation with X-Magento-Tags

Magento tags cached pages with product, category, and CMS block identifiers. This lets Varnish purge only affected pages when content changes, rather than flushing the entire cache. According to Adobe’s own documentation on Varnish integration, tag-based purging is what makes frequent catalog updates practical at scale. This matters because a full cache flush after every product update would erase the performance benefit Varnish provides. We used this tag-based purging on a client’s high-SKU-count catalog last quarter. Cache hit rate stayed stable even during frequent inventory updates.

Using ESI for Dynamic Content Blocks

Edge Side Includes let Varnish cache most of a page. Genuinely dynamic sections, like the mini-cart, stay uncached and render separately. According to Varnish Software’s own ESI documentation, this approach avoids the common tradeoff between full-page caching and per-visitor dynamic content. I implemented ESI for a client’s mini-cart last month. This let the surrounding category page stay fully cached while cart contents still updated correctly per visitor.

Verifying Varnish Is Actually Working

Checking the X-Magento-Cache-Debug response header confirms whether a page is served from cache or passed through to PHP. I check this header on every Varnish setup I configure. The admin panel showing Varnish as “enabled” doesn’t guarantee it’s actually caching anything. That setting only reflects Magento’s own configuration, not Varnish’s real behavior.

Varnish cache checkIs Varnish Actually Caching Your Pages?We’ll check your VCL, backend port, TTLs and cache headers, and confirm your pages are really served from cache.Check My Varnish Setup →

Common Varnish Configuration Mistakes

These 2 mistakes account for most of the Varnish issues I find during a typical 1-hour audit.

Assuming Enabled Means Working

Turning on Varnish in the admin panel is only the first step. This is because the admin setting controls Magento’s side only, not Varnish’s own configuration. I found this exact gap on the client site from our earlier example. Varnish looked correctly enabled but was pointed at the wrong backend entirely.

Caching Pages That Shouldn’t Be Cached

A custom VCL edit, made without understanding Magento’s default exclusions, can accidentally cache a customer-specific page. I’ve seen this mistake surface 2 to 3 weeks after a VCL change, not immediately. Testing cart and account pages after any VCL change catches this before it reaches customers.

How to Measure the Impact

Compare response time for the same cached page before and after the Varnish change. Use the X-Magento-Cache-Debug header to confirm cache status each time. Cache hit rate, visible through Varnish’s own statistics tools, is the clearest ongoing measure of whether the configuration is working.

Summary Table

Most of these fixes can be completed within 1 to 2 days, start to finish.

Area Why It Matters Typical Fix Time
Enabling in admin Required first step, but not sufficient alone Under 30 minutes
VCL file placement Varnish must actually use the exported file Under 1 hour
TTL settings Balances freshness against cache hit rate 1 to 2 hours
Page exclusions Prevents customer-specific data leaking Under 1 hour
Tag-based purging Avoids full cache flushes on every update Built into Magento by default

Conclusion

Magento 2 Varnish cache delivers its full benefit only when configured correctly, not just enabled. Confirm the VCL file is actually in use, set sensible TTLs, and verify cart and checkout stay excluded. Do this ideally within the first 1 week after setup. For Redis and object cache configuration, see our guide to configuring Redis in Magento 2. Our complete Magento 2 backend optimization guide covers the full picture. Our Magento speed optimization services can audit your Varnish setup directly.

Frequently Asked Questions

Q1. What is Varnish cache in Magento 2?

A full-page caching layer that serves cached HTML before requests reach PHP, cutting response time well below Magento’s default cache.

Q2. Does enabling Varnish in the admin panel mean it’s working?

Not necessarily. The VCL file must be correctly generated, placed, and referenced by Varnish, or the admin setting alone does nothing.

Q3. How do I check if Varnish is actually caching my pages?

Check the X-Magento-Cache-Debug response header. A value of HIT confirms the page was served from cache, not passed to PHP.

Q4. Should cart and checkout pages be cached by Varnish?

No, never. Magento’s default configuration excludes them automatically, since caching customer-specific content would leak data between visitors.

Q5. What’s the difference between Varnish and Redis in Magento?

Varnish handles full-page cache, serving entire HTML pages. Redis handles object and session cache. Most stores benefit from using both.

Q6. How does Varnish know when to purge cached pages?

Magento tags pages with product and category identifiers. This lets Varnish purge only affected pages when content changes, not the entire cache.

Q7. What is ESI used for in a Varnish setup?

Edge Side Includes let specific dynamic sections, like a mini-cart, stay uncached. The rest of the page stays fully cached.

Q8. Can a misconfigured VCL file make Varnish slower than no cache at all?

Rarely slower, but it can make Varnish effectively useless, caching nothing while the admin panel still shows it as enabled.

Q9. How often should I verify my Varnish configuration is still working?

At least quarterly, since a VCL update or server migration can silently break caching without an obvious error appearing.

Q10. How much faster are Varnish-cached pages compared to PHP-rendered ones?

Cached response times commonly drop to under 200 milliseconds, compared to several seconds for a fully rendered PHP page.

Done-for-you Varnish setupLet Our Team Tune Your Varnish CacheWe fix the VCL, TTLs, exclusions and purging, so pages come from cache, not PHP.Talk to Our Team →

Right Read More: How to Speed Up a Slow Magento 2 Store: 20 Proven Performance Optimization Tips

Right Read More: How to Benchmark Magento 2 Performance Before and After Optimization

Logo

About the author

Meenakshi Nahar

I’m a Full Stack Developer and the founder of W3SpeedUp, with over 10+ years of experience in web development, website speed optimization, Core Web Vitals, and technical SEO. My focus is helping businesses create faster, high-performing websites that improve user experience, search rankings, and conversions. Through this blog, I share actionable insights, optimization strategies, and real-world expertise gained from working with websites across multiple industries.

View all posts →
Review Details
W3 SpeedUp
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

×

    Get Free Audit Report