How can we help you today?

How to Configure Varnish for Magento 2.x

Created by: Jesper Verkade

Modified on: Wed, 17 Jun, 2020 at 4:01 PM

Customers with Hypernode Professional and Excellence plans can use Varnish to boost their Magento shop. This article explains how you can configure Varnish for your Hypernode. Do you have a Magento 1 shop, please check this article.

Although Varnish is extremely awesome when it get's to speeding up websites, Varnish is a complex technique that needs some experience to set it up. Don't implement varnish on production nodes but use a development node or the Hypernode Docker.

Enable Varnish for Magento 2.x

As Magento 2 supports Varnish out of the box, there is no need for the turpentine extension anymore in Magento 2. Simply follow the steps below to configure Varnish for Magento 2.

All Customers: Via the hypernode-systemctl CLI Tool

Activating Varnish and set which version to use on your Hypernode can be done via the hypernode-systemctl tool.

Dutch Customers: Via Your Service Panel

Only Dutch customers can also easily activate Varnish via the Service Panel. Go to Instellingen and change Varnish to Actief. Once you’ve changed this setting and enabled Varnish in Magento, Varnish will store all information in it’s own cache. Only when Varnish needs data that hasn’t been cached it will connect with the server.

International Customers: Via Your Control Panel

English speaking customers who ordered their Hypernode via have access to their control panel. Find your Hypernode, click on Settings and go to Caching. There you can enable Varnish with 1 click on the button.

Configure Magento 2.x for Varnish

  • Log in to the Magento Admin/Backend as an administrator.
  • Navigate to Stores > Configuration > Advanced > System > Full Page Cache
  • From the Caching Application list, click Varnish Caching
  • Enter a TTL value
  • Expand Varnish Configuration and insert the correct information:
    • Backend Host:
    • Backend Port: 8080
  • Save your VCL by clicking the button Save config in the top right
  • Click Export VCL for varnish 4

Configure Your Backend Servers Through the Commandline

If you want to flush the Varnish cache from the Magento backend, you need to add the Varnish server in your Magento config to http-cache-hosts


To do this, run the following command:

cd /data/web/magento2
chmod 750 bin/magento
bin/magento setup:config:set --http-cache-hosts=

Now when you flush your caches in cache management, your varnish full_page cache will be flushed too.

Test and Upload Your VCL

After downloading your VCL, check (in notepad or something similar) if the following configuration is present:

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

If your VCL checks out, upload it to your Hypernode (using SCP, FTP or FTPS or whichever client you prefer)

Remove Health Check Probe from Configuration

Note: The default VCL might have the following configuration:

backend default {
.host = "localhost";
.port = "8080";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;

Make sure you change this to the aforementioned configuration (without the health_check probe), since this will break on our Nginx configuration and will therefore result in a 503 Guru Meditation error.

Import Your VCL into the Varnish Daemon

Import your VCL into Varnish and save as mag2:

varnishadm vcl.load mag2 /data/web/default.vcl

The output should say: your VCL is compiled. If you receive a Permission denied error, and have recently activated Varnish, please close all ssh sessions, and log back in to reload your new permissions.

Now tell Varnish to activate the loaded VCL:

varnishadm vcl.use mag2

In the examples we used the name ‘mag2’ for our VCL, but you can use any name you prefer.

Test if the Correct VCL is Uploaded

List all VCL’s with the following command:

varnishadm vcl.list

The VCL you just imported and activated should have the status active. If all went well, varnish is now functioning with a working VCL.

Now to make sure this VCL is loaded after a reboot, make sure the "boot" vcl is thrown away:

varnishadm vcl.discard boot

Flush Your Varnish Cache When Using Magento 2

To flush the Varnish cache of your Magento store on the command line you can use /data/web/magento2/bin/magento cache:flush full_page This will purge the Varnish cache of the local Varnish instance.

Additionally you can flush your cache through the Magento admin backend or use varnishadm directly:

varnishadm "ban req.url ~ ."

Or if you want to flush the cache for a single domain in a multisite setup:

varnishadm "ban =="

Warming Your Cache

Magento 2 doesn't need to use turpentine anymore, so we can't use the cache crawler provided with turpentine. As an alternative you can use an extension to warm your cache or the cache warmers we provided on our Github account, here and here.

Enable Debug Headers

If you are implementing Varnish on Magento 2, you might want to view some caching headers that indicate whether the page is cacheable or not. To do this, put your Magento install in Developer mode. Now if you request a page through curl, you can see the X-Magento-Cache-Debug header:

curl -I -v --location-trusted '' > /dev/null | grep X-Magento

Which will show the debug headers:

X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400
X-Magento-Cache-Debug: MISS


  • If you are receiving Permission denied errors while running varnishadm or other varnish cli commands, and you have just activated Varnish, close any existing ssh sessions, and log back in to reload your updated permissions.
  • If your Varnish setup is not working over SSL, check this article
  • There is a bug when Varnish is activated on Magento 2.2.0, resulting in a "503 backend fetch" error. Please see Magento Github issue 10165. For now, we advise you to either wait with upgrading to Magento 2.2.0 when using Varnish until this bug is fixed or use an adjusted .vcl as a temporary workaround:
Jesper is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.