Skip to content

How to Set Up Varnish Full Page Cache for Magento 2

To use Varnish as Magento 2's full page cache, set Caching Application to Varnish Caching, generate a VCL file with bin/magento varnish:vcl:generate, and register your Varnish servers with --http-cache-hosts so Magento can purge them. Varnish then serves cached pages before requests reach PHP, while checkout and account pages stay uncached.

To run Varnish as Magento 2's full page cache, set the caching application to Varnish Caching, generate a VCL file from Magento itself, and tell Magento where Varnish lives so it can purge pages when content changes. Once that's done, cached pages come straight from Varnish and never touch PHP.

This guide covers Magento Open Source and Adobe Commerce on your own infrastructure. Adobe Commerce on Cloud uses Fastly rather than Varnish, so it doesn't apply there.

Why use Varnish instead of the built-in full page cache?

Magento's built-in full page cache stores rendered pages in its cache backend, but every request still reaches PHP so Magento can look the page up and return it. Varnish sits in front of Magento and answers from memory when it has the page. That's why Adobe recommends Varnish for production.

Built-in full page cache Varnish
Where pages are stored Magento's cache backend (files, or Redis/Valkey) Varnish memory
Does the request reach PHP? Yes — Magento runs to return the cached page No, when the page is cached
Invalidation Magento cleans its own backend Magento sends purge requests to Varnish
Best for Development and small stores Production, per Adobe's recommendation

Speed has a measurable business effect: in Deloitte's study for Google, a 0.1-second improvement in mobile site speed increased retail conversions by 8.4%. For the cache types Magento uses underneath, see the database and performance lesson (Arabic, with English code).

Which Varnish version works with Magento 2?

Adobe's system requirements for the latest patches, 2.4.6 through 2.4.9, list Varnish 8; earlier patches listed Varnish 7.7. There's a catch: Magento's VCL generator only includes templates for Varnish 4, 5, 6 and 7. There is no Varnish 8 template.

In practice, generate the VCL with --export-version=7 and test it thoroughly on staging, because Adobe doesn't explicitly document that combination.

How do you enable Varnish in the admin or CLI?

In the admin, go to Stores › Settings › Configuration › Advanced › System › Full Page Cache and set Caching Application to Varnish Caching. The same screen holds TTL for public content and the Varnish Configuration fields.

From the CLI, which suits production and deployment scripts:

bash
bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2

So Magento can purge the cache, register your Varnish server, or several separated by commas:

bash
bin/magento setup:config:set --http-cache-hosts=192.0.2.10:6081

How do you generate the right VCL file?

bash
bin/magento varnish:vcl:generate \
  --export-version=7 \
  --access-list=192.0.2.20 \
  --backend-host=192.0.2.20 \
  --backend-port=8080 \
  --grace-period=300 \
  --output-file=/etc/varnish/default.vcl
  • --access-list: hosts allowed to send purge requests, meaning your Magento servers. From 2.4.9 it accepts CIDR ranges.
  • --backend-host and --backend-port: the web server running Magento behind Varnish.
  • --grace-period: how long Varnish keeps serving a stale copy while it fetches a fresh one; the default is 300 seconds.
  • Without options, the defaults are localhost, port 8080 and export version 6.

Reload Varnish after generating the file so it picks up the new configuration.

What does grace mode protect you from?

When a cached page expires, Varnish doesn't make the visitor wait. It keeps serving the stale copy briefly while fetching a fresh one from Magento. That prevents a burst of PHP load when many pages expire at once, and keeps the store responsive if the backend slows down for a moment.

What should Varnish not cache?

  • Checkout and customer account pages: any block marked cacheable="false" makes its whole page uncacheable, which is intentional here. Customer-specific data such as the cart loads after the page through JavaScript.
  • Static files: the default VCL passes them through uncached. Adobe suggests caching them in Varnish only when you have few locales and no CDN; otherwise a CDN is the better fit.

Watch for this trap: cacheable="false" on a block in a sitewide layout such as default.xml disables caching on every page. If Varnish caches nothing, check for that first. It's a common interview scenario, covered with similar cases in the Redis, RabbitMQ and indexing lesson (Arabic).

How do you check Varnish is actually caching?

  • Request the same page twice with curl -I and look at the Age header. A value above zero on the second request means the response came from cache.
  • Run varnishlog or varnishstat on the Varnish server to see hits and misses.
  • Give Varnish enough memory to hold your most-visited pages, or it will evict important pages early.

To see where Varnish fits in the wider architecture, from load balancer to database, read the e-commerce system design lesson (Arabic). If you're also moving Magento's own cache to Valkey, see moving Magento from Redis to Valkey.

What changed in Magento 2.4.9?

  • Magento normalizes the order of query parameters before caching, so the same URL with parameters in a different order no longer creates a duplicate cache entry.
  • The purge access list accepts CIDR ranges.

Varnish production checklist

  • Caching Application is set to Varnish Caching
  • --http-cache-hosts lists every Varnish server
  • The VCL is generated with --export-version=7 and tested on staging
  • The access list contains only your Magento servers
  • A grace period is set
  • No sitewide layout contains a cacheable="false" block
  • A second request for the same page returns an Age above zero
  • Varnish has enough memory for your most-visited pages

Sources

Technical claims in this article were checked against:

  1. Adobe — Configure Varnish for Commerce
  2. Adobe — Performance best practices: configuration
  3. Adobe — System requirements
  4. Adobe Commerce 2.4.9 release notes
  5. Magento source — GenerateVclCommand.php (2.4-develop)
  6. Varnish documentation — Grace mode
  7. web.dev — Milliseconds make millions (Deloitte for Google)

Frequently asked questions

Does Adobe Commerce on Cloud use Varnish?

No. Adobe Commerce on Cloud uses Fastly instead of Varnish. The steps in this guide apply to Magento Open Source and Adobe Commerce running on your own servers.

Which Varnish version should I use with Magento 2.4.9?

Adobe's system requirements list Varnish 8, but varnish:vcl:generate only ships templates up to version 7. Generate the VCL with --export-version=7 and test it on staging before production.

How do I generate a Varnish VCL file in Magento 2?

Run bin/magento varnish:vcl:generate with --export-version, --access-list, --backend-host, --backend-port and --output-file. The Full Page Cache settings in the admin can also export the file for Varnish 6 and 7.

Why is Varnish not caching any Magento pages?

The most common cause is a block with cacheable="false" in a sitewide layout such as default.xml, which makes every page uncacheable. Also confirm Caching Application is set to Varnish and Varnish is running the VCL that Magento generated.

Do I need Varnish if I already use a CDN?

Usually yes. A CDN serves static assets and media, while Varnish caches the HTML pages themselves in front of Magento so requests never reach PHP. The two complement each other.

Related lessons (Arabic)

These free lessons are written in Arabic; code and technical terms are in English.

Written by

Abdulrahman Masoud

Questions about this article, or need help with a Magento store? Message me.