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.

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.

All customers: via the hypernode-systemctl 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 their 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.

English speaking customers: via your control panel

English speaking customers who ordered their Hypernode via Hypernode.com have access to a 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 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:

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:

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:

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:

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:

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:

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:

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:

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

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.

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:

Which will show the debug headers:

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:

615