What is Varnish?

Varnish is a high-performance HTTP reverse proxy designed to cache and serve web content extremely fast.
It sits between the client (browser) and your backend server (like nginx or Apache), and serves cached responses without having to hit PHP or MySQL every time.

📍 Typical request flow with Varnish:

  1. The browser sends an HTTPS request
  2. A web server (e.g. nginx on port 443) handles SSL termination
  3. The decrypted request is passed to Varnish on port 80
  4. If the page is cached (hit), Varnish responds directly
  5. If not (miss), it queries the backend (e.g. nginx on port 8080), which executes PHP-FPM (often on port 9000 or via socket)

⚠️ Varnish does not handle SSL. It must sit behind a reverse proxy that terminates TLS (like nginx or Traefik).

Configuring Varnish with Magento 2

Magento 2 offers native support for Varnish as a Full Page Cache (FPC) backend.

📦 Basic setup:

  1. Go to Magento Admin:
    Stores > Configuration > Advanced > System > Full Page Cache
  2. Select Varnish as the caching application
  3. Click “Export VCL” to download the configuration file

Magento provides a ready-to-use .vcl file that you can plug into your Varnish setup.

🔧 Understanding the VCL file

The .vcl file defines how Varnish behaves:

  • backend servers
  • cache logic
  • TTLs and headers
  • cookie handling
  • purge rules

👉 Official Varnish documentation:
https://varnish-cache.org/docs/

In Magento’s exported VCL file, key components include:

🔹 Backend definition

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

This tells Varnish where to forward requests when it has no cached version.

🔹 Purge ACL

acl purge {
  "127.0.0.1";
  "localhost";
  "IP publique du serveur";
  "IP du backend";
}

Only these IPs can purge the Varnish cache.


⚙️ Configuring Magento to work with Varnish

env.php : Varnish hosts for purge

Magento keeps track of the Varnish servers to notify for cache purge in the following config:

'http_cache_hosts' => [
    [
        'host' => 'varnish',
        'port' => '6081'
    ]
]

Port 6081 is used by default for PURGE requests (not for regular traffic).

💡 Using Traefik in front?

If you’re using Traefik as your main reverse proxy, there’s no need to pass requests through nginx before reaching Varnish.

➡️ You can route requests directly to Varnish, which then handles caching and, if needed, queries the backend.
This saves time and avoids unnecessary routing.

🏎️ Why is Varnish faster than Magento’s native FPC?

Magento offers FPC stored:

  • in filesystem (default)
  • or in Redis

But in both cases, Magento still has to be partially executed to return the cached response.
With Varnish, the cache sits entirely upstream — no PHP execution and no database access.

With Varnish:

  • No Magento bootstrapping
  • No PHP execution
  • No MySQL queries

Result: pages load in milliseconds, and your infrastructure can focus on real user actions (like buying stuff).

🎯 What not to cache with Varnish

Don’t use Varnish to cache images, media, or static files (JS/CSS). Why?

Magento media includes:

  • hundreds of static files (versioned, digested)
  • multiple product image sizes
  • theme-specific assets

📌 Use a CDN like Cloudflare for those files and configure cache rules there to handle image and asset delivery.

🧩 Handling personalized content?

Full Page Cache is great… unless you need to serve user-specific data:

  • customer-specific prices
  • cart or account-related blocks

Two common approaches:

  1. Use AJAX requests to load dynamic blocks after page load
  2. Use localStorage to manage client-side personalization

The right solution depends on the use case, UX goals, and security concerns.
👉 We’ll cover that in a future article.