Knowledge Base
MilesWeb / CMS FAQ

Learn to Configure Magento 2 Varnish in Easy Steps

Approx. read time : 5 min

A cache is an important component of a web server. Cache plays a key role in terms of application performance such as Magento as it decreases the load on the application to a good extent and increases the page loading speed.

Therefore, it helps in improving the user experience significantly.

Varnish is a popular cache used in Magento 2. The combination of Magento 2 and Varnish is proved to be useful and helps in improving your website performance. In order to integrate Varnish with previous versions of Magento, it is required to install the Magneto extensions such as “Turpentine Varnish Cache” or “Phoenix Page Cache”. Varnish cache is by defaulkt integrated with Magento 2 and needs minor changes in the configuration to help it work with your store.

What is Varnish Cache & How Does Magento Varnish Work?

With the help of Varnish cache the speed of web applications gets boosted. Technically, Varnish is called as HTTP accelerator and as per the name it accelerates the dynamic pages that contain heavy content for improving the overall performance of the Magento 2 stores.

Generally, fragment files are built by Magento Varnish stored in the memory. The consumption of time and network bandwidth for upcoming requests gets eliminated with Varnish.

Image Source: Magento DevDoc

When requests are processed, HTTP manages all calls for HTML, CSS, JavaScript and images. Varnish helps to proxy these requests for web server.

While the web server responds to these requests, the cacheable assets are stored by Varnish as well as any following user request is look after by Varnish. So, the web server doesn’t need to process the same requests again and again.

Note: After a configurable interval the assets cached by Varnish can expire and might get replaced by newer version.

The cache can also be cleared manually by accessing the Magneto Admin or by running the Magneto cache with the help of clean command via SSH.

Benefits of Varnish Cache for Magento Stores

When Varnish is integrated, Magento doesn’t need to commit time and resources to serve duplicate requests.

Therefore, the user experience improves essentially as per the user perspective with the requests being processed much faster and the pages being load much faster in their browsers.

Did you ever think that how Varnish Cache can help you gain more traffic and boost conversion rate?

Increase Sales

The page load time gets reduced due to Varnish cache and the content gets delivered 10X faster. The interesting fact is that up to 79% of customers that aren’t satisfied with a website’s performance are doubtful to buy from the same site again. You have a great chance of increasing your online sales with proper Magento Varnish configuration.

SERP Growth

Visitors spend more time as well as shop more often on websites that load faster. This improves the store’s SERP ranking, gaining more traffic and a better brand reputation. So, you can see that it is good to enable Magento Varnish Cache.


With the Magento Varnish Cache, you get an access to Varnish Configuration Language (VCL) that helps in customizing the requests as the Varnish handles the incoming and outgoing requests.

Multi-Layer Server

One or more Varnish servers can be added for enabling the high availability of a cluster processing solution. In this type of configurations, if the server doesn’t respond, the Varnish cache takes over and serves the cached assets which consistently help in maintaining the quality of the user experience.

Prerequisites for Magento 2 Varnish

While configuring Magento 2 Varnish, it is needed to check for the version compatibility factor because Magento 2 supports Varnish versions such as 4.x, 5.2 or 6.2.

Below are the steps to set up Magento 2 Varnish for your store:

  • Install Varnish
  • Configure Magento Varnish
  • Verify operation of Varnish With Magento

Magento 2 Varnish Configuration

It starts with a few changes to be done in the default configuration from the Magento Admin panel so that the Magento application can use Varnish properly.

Navigate to Stores > Configuration > ADVANCED > System > Full Page Cache. For Caching Application select Varnish Cache from the list named as Caching Application.

Alter the value in TTL for public content, in case you want to increase (or decrease) the lifetime value of public content cache.

Then, expand the Varnish Configuration and add the below details to enable advanced options:

  1. In the Access list, mention the IP or Host allowed to purge the Varnish Cache. Then add the domain name of your application in this field.
  2. Specify the Backend Host (in this example, it’s a local Apache server). If you leave the field empty it will be saved as localhost by deafult.
  3. Specify the Backend Port of the Apache server.
  4. Finally, specify the Grace period (which detects the time for which Varnish can serve stale content in case the backend is not responding).
  5. After filling all the fields, click on the Save Config button.

For activating Magento 2 Varnish via command line interface you can use SSH. Use the below commands:

bin/magento config:set--scope=default--scope-code=0system/full_page_cache/caching_application 2

Process to Verify Varnish With Magento

Now, Varnish is configured for your application. For your information, Varnish is connected to the Magento app now. In case, you flush the Magento cache the Varnish cache too will get purged automatically. Purging and Magento Cache work hand in hand. So, in case you want to Flush Magento cache it will purge Varnish Cache too automatically.

If you restrict the IP addresses that can purge the Varnish cache while configuring Varnish and want the same IP addresses to be allowed to flush the Magento cache, use the below command:

magento setup:config:set ‐‐http‐cache‐hosts=""

Replace the with your application domain.

Now, you can check Varnish headers to check if caching is working properly. Use cURL command or browser development tools and check for X-­Magento-­Cache-­Debug: header. Varnish is working properly, if you see HIT on the value. For seeing the Debug Header, you need to enable the developer mode in Magento 2. Add the below line in the .htaccess line for this:

php bin/magento deploy:mode:set {developer} [-s|--skip-compilation]

Bottom Line

It is a great optimization idea to integrate Varnish with Magento. Just by making few changes, you can start using Magento 2 Varnish. This is a simple step by step guide to set up Varnish with your store.

If you think, that there is something missing about the topic or have any recommendations for improving the content, please mention in the comment section below.

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna