Categories: Magento 2 BlogsMagento Tutorials, Tips and Guides

How to Setup Magento 2 With Varnish

Caching is an important part of any web server. With applications like Magento, it is more important to have caching as it drastically reduces the load on the application and speeds up the page response, thus yielding a better user experience. A popular option for Magento 2 is Varnish.

Magento 2 Varnish is a known combination and has proven to increase site performance significantly. With previous versions of Magento, we had to install plugins like “Phoenix Page Cache” or “Turpentine Varnish Cache” for the integration of Varnish with Magento, but in Magento 2, Varnish is integrated by default. It just needs a few configuration changes to make it work on your store.

To set up Varnish, you need to follow these steps:

Step 1: Varnish Setup

First, we have to make sure that Varnish is installed and working. This part is something that is out of the scope of this blog post, so we will assume that you have configured Varnish and it is in working state.

However, if you have your Magento store hosted on Cloudways Managed Magento platform then you don’t have much to worry. It offers one of the easiest processes and a reliable platform. To check whether Varnish is enabled or working on your Cloudways server, just go to your Server and click on Manage Services under Server Management.

Step 2: Magento Varnish Configuration

Now, we have to make a few changes in configuration from the Magento Admin panel so the application can use Varnish.

  • ­Go to STORES > Configuration > ADVANCED > System > Full Page Cache. Click the dropdown box of “Caching Application”. Choose “Varnish Caching” from the list.

  • ­Change the value to your required one in “TTL for public content” if you want to increase (or decrease) the lifetime value of Public content cache.

  • Click Varnish Configuration to enable advanced options. Define the IP or Host that is allowed to purge the Varnish cache in “Access list”. Put your application’s domain name in this field.

Define the Backend Host which in this case is Apache and is running locally. You can leave it as localhost too.

Backend Port is the port of Apache. On Cloudways servers, Apache is running behind Varnish and is using Port 8081. Change the value in this section to 8081.

Varnish is now configured on your application. Varnish Purging works parallel to Magento Cache. So if you’ll Flush Magento Cache, it will automatically purge Varnish cache too.

  • Restricting Purge host in Magento (Optional): In the above part where we restrict Purge IP in Access list, we can restrict those hosts on Magento’s env.php too. Issue the following command with appropriate domain name to make the changes:
magento setup:config:set ‐‐http‐cache‐hosts="example.com"

You can check Varnish headers if you want to see if caching is working properly. Use any developer tool or Curl command and look for “X-­Magento-­Cache-­Debug:” header. If you are seeing “HIT” as the value of mentioned header, then it’s working. Note that you have to enable developer mode in Magento 2 if you want to see the Debug Header. You can do it by adding following line in your .htaccess.

SetEnv MAGE_MODE developer

Final Thoughts

Integrating Varnish configuration with Magento eases the process. With minimal changes you can start using the Varnish with Magento. At the same time, Cloudways further simplifies the entire process.

Syed Maad Jahangir :Syed Maad Jahangir worked as Senior System Engineer at Cloudways.