Magento 2 Varnish Cache Configuration

What is Varnish cache?

The Varnish cache is an HTTP accelerator (sometimes it’s called a reverse HTTP proxy) enabling which you are able to handle much more traffic without website’s speed or performance come-down due to the reduction of the loading time. The cache option is also useful when working in the production mode, as the variant is much faster and allows for caching of most of the website content.  The solution is good for websites of various size as it’s developed for web performance betterment. However, the technology is pretty confusing and requires some technical knowledge.

Note: Learn how to install varnish on Magento 2 in the official documentation.

How to configure the Varnish Caching?

After the Full Page Cache is configured and Varnish Caching is chosen in the Caching Application field:

caching-application-varnish

 

you need to make the additional configuration in the section:

 varnish-configuration

 

 

  • Access list

Enter IPs access lists that can purge Varnish configuration. If you leave the field unchanged a default localhost will remain there.  

  • Backend host

Add the backend host. If you leave the field unchanged a default localhost will remain there.

  • Backend port

Indicate the backend port here.

  • Export configuration field

Choose the Varnish version you use.

Once it’s done, click the Save Config button.

Export a Varnish Configuration File

The action is optional and required in case of the backend host or/and port number change that has been defined during the Magento 2 Varnish setup. 

  • Log in to your Admin Panel;
  • Choose one of the options here Stores> Configuration> Advanced> System> Full Page Cache> Varnish Caching> Export VCL for Varnish 3 or 4 in Magento 

  export-configuration

 

  • The existing Magento varnish VCL should be replaced with the VCL that has been just exported.
  • Open the default.vcl and place the IP of the Varnish host in the acl purge value:

E.g.: acl purge {

  "localhost";

}

  • Then Varnish and the web server should be restarted:

service varnish restart

service httpd restart

  • After that verify the actions to make sure the Varnish is working.

How to verify the Varnish is working after the export?

  • First off, you need to verify HTTP response headers:

In the Magento developer mode run curl (other utility) to see response headers:

E.g.: curl -I -v --location-trusted 'http://192.0.2.55/magento2'

Valuable headers: X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400

Age: 0

X-Magento-Cache-Debug: MISS

  • Secondly, review the page load times. When Varnish operates well less than 150ms should be left for a page with cacheable blocks.
  • At last, check the Magento Cache. The <your Magento install dir>/var/page_cache directory should be empty. You can prove it through log in to Magento as a file system owner and type the following command: rm -rf <your Magento install dir>/var/page_cache/*. Then choose one of the cacheable pages and check the var/page_cache/ directory there. The directory should be empty. If you had to clear it, restart the Varnish.

Magento 2 Varnish Cache is also compatible with Redis, Nginx(SSL):

  • Redis: can operate together; both the Redis and Varnish cache remains invalidated after the content is updated. So, there is hardly any sense in it.

  • Nginx (SSL): as long as SSL/TSL doesn’t support Varnish, with the help of Nginx you can solve the issue, which means you can enable Magento 2 Varnish cache while using the SSL. 

See also: Learn how to configure the Flush Magento 2 Cache button to purge Varnish in Magento 2 documentation.

Having made everything correctly, you are still dissatisfied with the result, run the Magento 2 Full Page Cache Warmer extension. It will help you with smooth cache management. 

For faster pages loading, try our Google Page Speed Optimizer

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.