Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  CMS FAQ  >  Learn to Configure Magento 2 Varnish in Easy Steps
Top Scroll

Learn to Configure Magento 2 Varnish in Easy Steps

 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.

Flexibility

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="example.com"

Replace the example.com 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.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.