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 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:
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.
To check whether Varnish is enabled or working on your Cloudways server, go to your Server and click on Manage Services under Server Management.
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.
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.
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:
magento setup:config:set ‐‐http‐cache‐hosts="example.com"
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
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.
Convert visitors into buyers on your 100% faster Magento store.
Deploy your Magento stores on optimized Magento hosting servers.