OCMLABS/MODULE-CLOUDFLARE · VARNISH

Running Cloudflare in front of Varnish, with no VCL change

  • Magento
  • Adobe Commerce
  • Hyvä

Nothing to change in Varnish

Magento’s stock Varnish VCL rewrites Cache-Control to no-store, no-cache, must-revalidate, max-age=0 on every page in vcl_deliver. Cloudflare in front of it would see nothing it may cache.

From version 1.0.7 the module goes around that. In Varnish mode it also sends Cloudflare-CDN-Cache-Control: max-age=<ttl> on pages it marks cacheable. No Magento VCL touches that header:

  • Varnish stores it with the page, so cache hits carry it too.
  • Cloudflare decides from it and ignores Cache-Control.
  • Cloudflare never passes it on, so browsers still get Magento’s no-store and always revalidate.

This works with stock VCL and with customised VCL (a load balancer health endpoint, a store in a subdirectory, a different backend port) alike. You do not need to export, merge or reload anything.

What the module does in Varnish mode

When Magento’s caching application is set to Varnish (Stores > Configuration > Advanced > System > Full Page Cache):

  • It leaves Magento’s own headers and cookies to Varnish. No Cache-Control rewrite and no cookie stripping happen in PHP, because Varnish already handles both.
  • It adds the Cache-Tag header for purge by tag, and its internal edge marker.
  • It adds Cloudflare-CDN-Cache-Control only where edge caching is safe: a page it marked, that Magento sends as public, that is not a bypass path, with no X-Magento-Vary cookie on the request or the response.

Edge caching in Varnish mode covers default-context pages only. Vary Handling Cache key is not supported behind Varnish; leave it at Bypass. Requests with the vary cookie are kept off the edge by the “Bypass on vary cookie” cache rule.

CAUTION /

Varnish keeps objects it stored before a change until they expire or are purged. After upgrading to 1.0.7, turning edge caching on or off, or changing Bypass Paths, flush Magento’s cache once (System > Cache Management > Flush Magento Cache, or bin/magento cache:clean full_page). That purges Varnish, and Cloudflare too when Purge Everything on Full Page Cache Clean is on.

Checking it works

Request a storefront page through Cloudflare twice:

Terminal window
curl -sI https://www.example.com/some-category.html | grep -iE "cf-cache-status|cache-control"

The second request should show cf-cache-status: HIT. Cache-Control may still read no-store, no-cache, must-revalidate, max-age=0; that is what browsers get, and it is expected.

To see what Cloudflare receives, ask the origin directly:

Terminal window
curl -sI -H "Host: www.example.com" http://varnish-host/some-category.html | grep -iE "cloudflare-cdn-cache-control|cache-tag"

You should see Cloudflare-CDN-Cache-Control: max-age=... and a Cache-Tag line. The “Origin sends edge cache headers” health check does the same when Storefront Origin is set to the Varnish address.

Optional: the Cloudflare VCL

The module can still export a Cloudflare-aware VCL. It is Magento’s generated VCL with the edge decision also made in vcl_deliver: marked pages get Cache-Control: public, max-age=0, s-maxage=<ttl> and CDN-Cache-Control, and the internal marker header is removed. Load it only if you want those headers for caches other than Cloudflare and your VCL is stock, because it replaces the VCL you run.

Terminal window
bin/magento ocmlabs:cloudflare:vcl:export --varnish-version=7 --output=/tmp/default.vcl
varnishd -C -f /tmp/default.vcl > /dev/null

Match --varnish-version to varnishd -V; versions 4, 5, 6 and 7 are supported and 6 is the default. Download Cloudflare VCL on the Health tab produces the Varnish 6 file. Reload with varnishreload so Varnish keeps its cache. Where the VCL is loaded it has the final say: on pages it does not make edge cacheable it removes Cloudflare-CDN-Cache-Control again.

If you do not want HTML at the edge

Leave Enable Edge Caching of HTML off. Purging, the cache rules, the health checks and analytics all keep working; HTML stays on Varnish.

See also

In this module