This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Setup Magento 2 With Varnish

by Syed Maad Jahangir  July 11, 2016

Caching is an essential part of any web server. With applications like Magento, it is more important to have the cache 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

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 in your store.

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

  1. Install Varnish
  2. Magento Varnish Configuration
  3. Verify Varnish With Magento

Install Varnish

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 Hosting Platform, then you don’t have much to worry. It offers one of the most straightforward processes and a reliable platform.

Cloudways Magento Hosting For Developers

To check whether Varnish is enabled or working on your Cloudways server, go to your Server and click on Manage Services under Server Management.

Manage Server Cloudways

Magento Varnish Configuration

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

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

Cloudways Advance

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

Cloudways TTL

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.

Cloudways Varnish Configuration

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.

Verify Varnish With Magento

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 the Access list, we can restrict those hosts on Magento’s env.php too. Issue the following command with the appropriate domain name to make the changes:

You can check Varnish headers if you want to see if caching is working correctly. Use any developer tool or Curl command and look for “X-­Magento-­Cache-­Debug:” header. If you see “HIT” as the value of the 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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Syed Maad Jahangir

Syed Maad Jahangir worked as Senior System Engineer at Cloudways.

Stay Connected:

You Might Also Like...

  • VNP

    Thanks for the article. I tried all this, but I noticed ‘Flush Magento Cache’ does not purge Varnish.
    X-­Magento-­Cache-­Debug keeps showing HIT, changes (for example change some product description and flush magento cache) aren’t visible, unless you put some variable in the URL (/?showmetherealthing=1), disabling the cache. Which to me indicates that Varnish isn’t purged, but keeps sending the ‘old’ page.