How can we help you today?

How to Configure Varnish for Magento 2.x

Created by: Jesper Verkade

Modified on: Wed, 23 Sep, 2020 at 11:47 AM


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.

TABLE OF CONTENTS

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.

Enable Varnish via the hypernode-systemctl tool

hypernode-systemctl settings varnish_enabled --value


Enable Varnish via the Service Panel

  • Log in on the Service Panel
  • Go to the tab "Instellingen"
  • Click on "Varnish"
  • Use the switch to enable Varnish


Enable Varnish via the Control Panel

  • Click on "Hypernodes"
  • Click on "Caching"
  • Select the Hypernode 
  • Click on "Enable Varnish"


Configure Varnish on the Vhost

Since the introduction of hypernode-manage-vhosts Hypernode may work somewhat different than you might be used to. With HMV enabled, it requires one more step to configure Varnish for your shop/vhost. Remember, for each domain, there should be a vhost created. You can list an overview of all configured vhosts with hypernode-manage-vhosts --list. While you do that, note that there is a column, "varnish". By default this is set to "False". Which means that Varnish isn't configured for this vhost. You can configure Varnish for the vhost by running the following command:

hypernode-manage-vhosts EXAMPLE.COM --varnish


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: 127.0.0.1
    • 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=127.0.0.1:6081

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 = "127.0.0.1";
.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 req.http.host == example.com"

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 'example.hypernode.io' > /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

Troubleshooting

  • 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:
J
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.