Varnish on Magento 2

in Caching

Customers with Magento Go Big and Excellence plans can use Varnish to boost their Magento shop. This article explains how you can configure Varnish for your Hypernode.

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 a hypernode vagrant.

Configure 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

To get started using varnish, you first need to enable it in our Service Panel. This will trigger a server configuration update.
A few minutes after enabling varnish, you’ll be able to use it on your node.

Configure Magento for varnish

  • Log in to the Magento Admin 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)

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.

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

Magento2 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.

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 your varnish setup is not working over ssl, check this article

2