Adobe Commerce and Magento 2 Varnish configuration, 2.4.4 to 2.4.9
Home / Insights / Adobe Commerce and Magento 2 Varnish configuration, 2.4.4 to 2.4.9
Magento 2 Varnish configuration comes down to four steps on Adobe Commerce, Magento Open Source and Mage-OS: run a Varnish version Adobe supports for your release, put it in front of the web server, switch the full page cache to Varnish and load the VCL that Commerce generates with bin/magento varnish:vcl:generate, then tell Commerce where to send purges. The steps are the same from 2.4.4 through 2.4.9; what changes by release is the Varnish version and the VCL template. On Adobe Commerce on cloud infrastructure you skip all of it, because Fastly is the full page cache in Staging and Production.
This post is the setup. If you're still working out what the built-in cache, Varnish and Redis each do, start with Magento full page cache vs Varnish vs Redis and come back.
Which Varnish version does Magento 2 support?
Adobe lists one supported Varnish version for each release and patch in its system requirements (opens in a new tab). For on-premises sites, as of November 2026:
| Release line | Varnish at release | Newest patch Adobe lists | VCL templates you can export |
|---|---|---|---|
| 2.4.9 | 8 | 8 | 6, 7 |
| 2.4.8 | 7.6 | 8 at 2.4.8-p5 (7.7 for p1 to p4) | 6, 7 |
| 2.4.7 | 7.5 | 8 at 2.4.7-p10 (7.7 for p6 to p9) | 6 |
| 2.4.6 | 7.1 | 8 at 2.4.6-p15 (7.7 for p11 to p14) | 4, 5, 6 |
| 2.4.5 | 7.0 | 8 at 2.4.5-p17 (7.7 for p13 to p16) | 4, 5, 6 |
| 2.4.4 | 7.0 | 7.7 (p14 to p18) | 4, 5, 6 |
Three things the table doesn't say out loud:
- The template number isn't your Varnish version.
varnish6.vclsays its minimum is Varnish 6.0 andvarnish7.vclsays 7.0. On 2.4.7 and earlier, the 6 template is the newest you can export, including on patches where Adobe lists Varnish 7.x or 8. - There's no
varnish8.vcl. Adobe lists Varnish 8 for 2.4.9 and for the newest patches of 2.4.5 through 2.4.8, but the newest template is still the 7 export. Compile-check whatever you export against the Varnish you actually run (shown below) before it takes traffic. - 2.4.7 dropped Varnish 4 and 5. The export no longer offers them, so if a runbook still says
--export-version=4or5, it was written for an older site.
Mage-OS uses the same PageCache module and the same commands. Mage-OS 3.x and 4.0 are built on Magento Open Source 2.4.9 and Mage-OS 2.x on 2.4.8, so pick the template for that base. Mage-OS's own requirements page (opens in a new tab) recommends Varnish 7.7.
How do I configure Varnish with nginx for Magento 2?
Varnish speaks plain HTTP. Adobe's docs say it directly, "Varnish does not support SSL", and recommend an SSL termination proxy in front of it, such as nginx. A single-server setup usually looks like this:
| Port | What listens | What it does |
|---|---|---|
| 443 | nginx (TLS) | Ends HTTPS and passes the request to Varnish |
| 80 | Varnish | Answers cache hits, sends misses to the backend |
| 8080 | nginx or Apache running Commerce | Runs PHP and Commerce |
- Install Varnish from your OS packages or Varnish's own repository, then check the version with
varnishd -V. Adobe asks you to confirm it's a supported version before you go further. - Move the Commerce web server to port 8080: the
listendirective of the Commerce server block in nginx, orListen 8080and the virtual host in Apache. - Make Varnish listen on port 80. Where that's set depends on your packaging: a systemd unit,
/etc/default/varnishor/etc/varnish/varnish.params. - Put TLS in front and tell Commerce the original request was HTTPS:
server {
listen 443 ssl;
server_name shop.example.com;
# ssl_certificate and ssl_certificate_key go here
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
X-Forwarded-Proto isn't decoration. It's the default value of Commerce's Offloader header setting (Stores > Settings > Configuration > General > Web > Base URLs (Secure)), which is how Commerce knows the shopper is on HTTPS. The generated VCL also adds that header to the cache key, so the HTTP and HTTPS copies of a page never mix. If you rename the header in one place, rename it in the other.
How do I generate the Varnish VCL for Magento 2?
In the Admin, open Stores > Settings > Configuration > Advanced > System > Full Page Cache:
- Set Caching Application to Varnish Caching.
- Leave TTL for public content at 86400 seconds (one day) unless you have a reason to change it. Product, category and price changes purge the affected pages anyway.
- Under Varnish Configuration, fill in Access list (who may purge), Backend host and Backend port (your web server, for example 127.0.0.1 and 8080) and Grace period (300 seconds by default).
- Save, then click Export VCL for Varnish 7 on 2.4.8 and 2.4.9, or Export VCL for Varnish 6 on earlier releases.
From 2.4.7 you'll also see Handles params size (default 100). It caps how many layout handles the page_cache/block/esi endpoint will process; Adobe added it in 2.4.7 to reduce the risk from that endpoint.
The same from the command line:
bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2
bin/magento varnish:vcl:generate \
--export-version=7 \
--access-list=127.0.0.1 \
--backend-host=127.0.0.1 \
--backend-port=8080 \
--grace-period=300 \
--output-file=/tmp/default.vcl
caching_application is 1 for the built-in cache and 2 for Varnish. One catch with varnish:vcl:generate: it doesn't read what you saved in the Admin. Each option has its own default (localhost, port 8080, grace 300, export version 6), so pass them explicitly or you'll get a VCL for localhost:8080 built from the 6 template. From 2.4.7, --input-file builds the VCL from your own template (the path is relative to the Commerce root), so your changes can live in git instead of being patched in by hand after every export.
Install it as root, and let Varnish compile it before you restart:
cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
cp /tmp/default.vcl /etc/varnish/default.vcl
varnishd -C -f /etc/varnish/default.vcl > /dev/null && systemctl restart varnish
varnishd -C compiles the VCL and exits, so a syntax error stops you before the restart does.
Read two parts of the file before you restart:
acl purgehas to list the addresses PURGE requests come from, which means the server or servers running Commerce. On one box that'slocalhost; with separate web nodes, it's their IPs. A PURGE from anywhere else gets405 Method not allowed.- The health probe's
.url. The templates from 2.4.7 on probe/health_check.php, which assumes the web server's document root ispub/. Older templates probe/pub/health_check.php. If the probe gets a 404, Varnish marks the backend sick and you'll chase 503s that have nothing to do with load.
How does Commerce tell Varnish to purge?
Commerce purges Varnish by itself when you change products, categories, prices and promotional pricing rules, and when you clean or flush the cache. It just needs to know where Varnish is:
bin/magento setup:config:set --http-cache-hosts=127.0.0.1:80
List every Varnish host, comma-separated with no spaces: --http-cache-hosts=10.0.0.21:80,10.0.0.22:80. Without this setting, Commerce sends the PURGE over plain HTTP to the request's own host name on port 80. That only works when Varnish happens to answer there and the sender is in acl purge.
How do I check that Varnish is caching Magento pages?
Request the same page twice and read the headers:
curl -sI https://shop.example.com/ | grep -i -E 'x-magento-cache-debug|grace'
curl -sI https://shop.example.com/ | grep -i -E 'x-magento-cache-debug|grace'
The Commerce VCL sets X-Magento-Cache-Debug on every response: MISS the first time, HIT after that, and from 2.4.7 UNCACHEABLE when the response couldn't be cached at all. Developer mode adds Age and X-Magento-Cache-Control too, which is why Adobe's own check runs in developer mode.
What you won't see is X-Varnish, Via or Server, because the Commerce VCL removes them. An empty grep X-Varnish doesn't mean Varnish is off, so don't use it as the test.
Three more checks, from Adobe's verification steps and Varnish itself:
var/page_cachestays empty while you browse cacheable pages. Files there mean Commerce is still using the built-in cache.- Cacheable pages load fast. Adobe's figure: a page with cacheable blocks, such as the home page or a category, should load in under 150 ms when Varnish serves it.
- Hits outnumber misses.
varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_missshows both counters since Varnish started.
Why does Varnish return 503 Backend fetch failed?
Adobe documents two causes.
The cache-tags header is too long. Commerce lists every product and category on a page in an X-Magento-Tags response header, and Varnish's default http_resp_hdr_len is 8 KB. A big category goes past it. Adobe's fix is to set it to your largest category's product count times 21 (each tag is about 21 characters), for example 65536 bytes for a category of about 3,000 products, and raise http_resp_size and workspace_backend to match:
-p http_resp_hdr_len=65536 \
-p http_resp_size=98304 \
-p workspace_backend=98304 \
These are varnishd startup parameters. They go wherever your system starts Varnish, not in the VCL.
The backend is slow or marked sick. The generated VCL already sets .first_byte_timeout = 600s, Adobe's fix for 503s on long requests. If you hand-wrote your backend block, add it. Then check the health probe: varnishadm backend.list shows whether Varnish thinks the backend is healthy, and a probe pointed at the wrong health_check.php path is the usual reason it doesn't.
Why isn't Varnish clearing the cache?
Work through these in order:
- No
http_cache_hosts. See the purge section above. This is the most common one. - The PURGE is rejected. Send one by hand from the Commerce server:
curl -X PURGE -H 'X-Magento-Tags-Pattern: .*' http://127.0.0.1:80/.200 Purgedis good,405means the sender isn't inacl purge, and400means the header is missing. - You cleaned the wrong cache type. Since 2.4.7,
bin/magento cache:clean configorcache:clean layoutno longer purges the full page cache. Runbin/magento cache:clean full_page, orcache:cleanwith no types. - The change hasn't been written yet. Admin mass attribute updates are saved by the
product_action_attribute.updatequeue consumer, so if consumers aren't running there's nothing to purge yet. Our guide to writing and running a Magento 2 queue consumer covers getting them going. - A module doesn't tag its pages. A block that shows data from its own tables needs its own cache tags, or Commerce can't know which pages to purge when that data changes. On one site we found Admin edits to attachments and web forms never cleared the cached pages that show them; now they do, with tests that wait for Varnish to cache a page.
Two things not to do. rm -rf var/page_cache/* does nothing for Varnish, because that directory belongs to the built-in cache. And restarting Varnish to clear it empties the whole cache and sends every request to PHP at once. Adobe's one restart rule is the other way around: if you delete var/cache by hand, restart Varnish.
Is anything on your pages getting past Varnish?
A full page cache only helps the requests it can cache. Some bypass it by design: the Commerce VCL passes anything that isn't GET or HEAD, plus URLs under /customer and /checkout, and the cart and the customer's name are loaded by the browser after the page arrives. The trouble is the requests that shouldn't be uncached. If product or category pages come back UNCACHEABLE, look first for a block with cacheable="false" in layout XML: one such block makes the whole page uncacheable. Our full page cache vs Varnish vs Redis post goes through the other reasons a page keeps missing.
What about Varnish on Adobe Commerce Cloud?
On Adobe Commerce on cloud infrastructure, you don't install or configure Varnish. Fastly (opens in a new tab), a Varnish-based CDN, is the full page cache and is required in Staging and Production. Integration environments don't have it. You set Caching Application to Fastly CDN, upload the Fastly VCL from the Admin, and make changes with Fastly custom VCL snippets rather than a default.vcl. To check it's working:
curl https://shop.example.com -vo /dev/null -H Fastly-Debug:1
Look for Fastly-Magento-VCL-Uploaded and X-Cache with a HIT in the response. If you only ever get MISS, Adobe's Fastly troubleshooting guide starts with checking that responses carry the X-Magento-Tags header and a Cache-Control max-age above 0.
Frequently asked questions
Which VCL should I export for Varnish 7 or 8?
On 2.4.8 and 2.4.9, export version 7 (--export-version=7, or Export VCL for Varnish 7 in the Admin). On 2.4.7 and earlier, version 6 is the newest template there is. There's no Varnish 8 template on any release, so compile-check the export with varnishd -C -f on the Varnish you run.
Does Varnish cache GraphQL requests?
Yes, GET queries only. Adobe's GraphQL caching docs (opens in a new tab) say POST queries aren't cached, mutations must be POST, and a request that includes cart, customer, order or wishlist data skips the cache. Since 2.4.4 the X-Magento-Cache-Id header lets Varnish or Fastly cache results for logged-in customers, keyed on store, currency, customer group and tax rate. Adobe says it can't be used reliably with the built-in cache.
Should Varnish cache static files?
Not by default. The Commerce VCL passes /static and /media straight to the web server. Adobe recommends a CDN for assets, and says caching them in Varnish can pay off when you serve few locales and your customers are near your servers. If you turn it on, restart Varnish after every deploy that changes assets.
Can I edit the generated default.vcl?
Yes, but every export overwrites it and upgrades change the template. 2.4.8 strips more marketing parameters from URLs (gad_source, srsltid and msclkid among them), and 2.4.9 sorts query strings so the same page isn't cached twice. From 2.4.7, keep your version in git, build from it with --input-file, and diff it against the new template on every upgrade.
Do I still need Redis if I run Varnish?
Yes, for a different job. Varnish holds pages. Redis, or Valkey on 2.4.9 and the newest patches, holds Commerce's own cache (configuration, layout, blocks) and sessions. Our full page cache vs Varnish vs Redis post covers which release supports which.
Is Varnish configured differently on Mage-OS?
No. Mage-OS ships the same PageCache module and the same bin/magento commands, so everything here applies. Match the template to the Magento Open Source release your Mage-OS version is built on: 2.4.9 for Mage-OS 3.x and 4.0, 2.4.8 for Mage-OS 2.x.
If your pages say MISS however often you load them, or a price change takes a day to show up, send us the URL and your version through Adobe Commerce support. Cache headers don't lie, and they usually tell us where to look first.