Magento 2 Varnish Cache: Ultimate Setup Guide (2026)
Why Varnish Cache Matters for Magento 2
Varnish Cache is a reverse proxy that sits in front of your Magento 2 store and serves cached pages to visitors — before the request even hits PHP. The result? Pages load in milliseconds instead of seconds.
For Magento stores with heavy catalog pages, Varnish can reduce server load by 80% and slash Time to First Byte (TTFB) from 1.5s to under 100ms. This is not just about speed — it directly impacts your Core Web Vitals scores, SEO rankings, and conversion rates.
How Varnish Works with Magento 2
Here is the flow when a customer visits your Magento store with Varnish enabled:
- Request arrives — Varnish checks if the page is in its cache
- Cache HIT — Varnish serves the page instantly (no PHP, no database)
- Cache MISS — Varnish forwards the request to Magento, caches the response, then serves it
- Cache invalidation — When you update a product or flush the Magento cache, Varnish purges affected pages automatically
Magento 2 generates X-Magento-Tags headers that tell Varnish exactly which pages to invalidate.
Step 1: Installing Varnish
On Ubuntu/Debian:
sudo apt-get update
sudo apt-get install varnish -y
Check the version (Magento 2.4.x supports Varnish 6.0 and 7.x; we recommend Varnish 7.3+):
varnishd -V
Step 2: Generating the Magento 2 VCL File
Generate the VCL from Magento CLI:
php bin/magento varnish:vcl:generate --export-version=7 --output-format=default > /etc/varnish/default.vcl
Configure the backend in the generated VCL:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Set Varnish to listen on port 80:
VARNISH_LISTEN_PORT=80
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
Step 3: Configure Magento to Use Varnish
Via CLI:
php bin/magento config:set system/full_page_cache/caching_application 2
php bin/magento config:set system/full_page_cache/varnish/access_list localhost
php bin/magento config:set system/full_page_cache/varnish/backend_host localhost
php bin/magento config:set system/full_page_cache/varnish/backend_port 8080
php bin/magento config:set system/full_page_cache/varnish/grace_period 300
php bin/magento cache:flush
Step 4: Move Web Server to Port 8080
Since Varnish now listens on port 80, configure Nginx/Apache to listen on 8080 instead, then restart both services:
sudo systemctl restart varnish
sudo systemctl restart nginx
Step 5: Verify Varnish Is Working
Check response headers:
curl -I https://yourstore.com/
Look for X-Magento-Cache-Debug: HIT and X-Varnish headers. A healthy store should have 80%+ cache hit rate. Monitor with:
varnishstat
Common Issues and Fixes
Session Cookies Preventing Caching
Third-party extensions may add cookies that bypass the cache. Add to your VCL:
sub vcl_recv {
if (req.http.cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID|admin|frontend)=", "; \\1=");
}
}
503 Errors from Varnish
Varnish cannot reach your backend. Verify the web server is running on port 8080:
curl -I http://localhost:8080/
Benchmark Results: Before vs After Varnish
Tested on a Magento 2.4.7 store with 10,000 products:
| Metric | Without Varnish | With Varnish | Improvement |
|---|---|---|---|
| TTFB | 1,200ms | 85ms | 93% faster |
| Page Load | 3.8s | 0.6s | 84% faster |
| Requests/sec | 45 | 2,800 | 62x more |
| Server Load | 85% | 12% | 86% reduction |
When Varnish Alone Is Not Enough
Varnish handles full-page caching brilliantly, but it does not optimize your assets (JS, CSS, images) or database queries. For a truly fast Magento store, combine Varnish with:
- Redis for session and backend cache storage
- PHP OPcache for compiled PHP code caching
- Image optimization (WebP conversion, lazy loading)
- JS/CSS minification and bundling
This is where Magefine Page Speed Optimizer comes in — it handles the asset-level and frontend optimizations that Varnish does not touch, giving you the complete performance stack.
Related tool from Magefine
Varnish handles server-side caching. Page Speed Optimizer handles everything else: minify JS/CSS, convert images to WebP, lazy load, defer scripts, and optimize your critical rendering path. Together they are the complete Magento 2 performance toolkit.
Buy now — €150 Try the live demo →