Guide

Set up Varnish for Magento 2, step by step.

Full-page cache in Varnish: switch Magento to it, generate the VCL, put Varnish between nginx and the store, and check that pages come from the cache. Checked on Nov 5, 2026.

Step 1Magento

Switch the full-page cache to Varnish

In the admin this is Stores, Configuration, Advanced, System, Full Page Cache, Caching Application. From the command line it is one setting:

On your server

bin/magento config:set system/full_page_cache/caching_application 2

1 is the built-in cache, 2 is Varnish.

Step 2Magento

Generate the VCL from Magento

Magento writes a Varnish configuration that knows its cache tags and purge requests. Point the backend at the port where nginx serves the store, and allow purges only from the server itself.

On your server

bin/magento varnish:vcl:generate --export-version=7 \
  --backend-host=127.0.0.1 --backend-port=8080 \
  --access-list=127.0.0.1 --output-file=/etc/varnish/default.vcl

bin/magento varnish:vcl:generate --help lists the export versions your Magento release offers.

Step 3Magento

Tell Magento where Varnish listens

When a product or page changes, Magento sends a purge request to Varnish. It needs the address and port Varnish listens on.

On your server

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

Step 4Server

Put Varnish between nginx and the store

Varnish does not speak HTTPS. The usual chain is: nginx on 443 ends TLS and passes the request to Varnish on 6081, and Varnish asks a second nginx server block on 8080 that runs PHP. Restart Varnish after changing the VCL.

nginx and Varnish

# nginx, the public server block (443)
location / {
    proxy_pass http://127.0.0.1:6081;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# then
systemctl restart varnish

Step 5Check

Check that pages come from the cache

Open a category page twice and watch the hit counter grow, or follow one request through Varnish. Then flush only the page cache when you need to.

On your server

varnishstat -1 | grep -E 'cache_hit |cache_miss '
varnishlog -g request -q 'ReqURL eq "/"'
bin/magento cache:flush full_page

A cache_hit counter that keeps growing on repeat visits means Varnish is serving the pages.

Questions, answered.

ShopWhizzy support Magento since 2008

Talk to us
Any free port; 6081 is the usual one. nginx sends requests to it, and Magento sends its purge requests to the same address.
They do different jobs. Varnish serves whole pages from memory before PHP starts; Redis or Valkey holds Magento's cache and sessions. Most fast stores use both.
Cart, checkout and account pages are private, so Magento marks them as not cacheable. Blocks marked cacheable="false" in a layout make the whole page uncacheable, which is a common cause of slow stores.
Yes. Every Magento plan runs Varnish from the first minute, with the VCL generated for your store.

Bring your store over. We move it for free.

Pick a plan, pick a platform, and your store is online before the coffee cools.

Prices include VAT. Server support on every plan.