Reading list Switch to dark mode

    Varnish Cache In Magento 2 (Adobe Commerce)

    Updated 4 March 2024

    What is Varnish

    Varnish is a reverse proxy server, which sits in-front of the web server and takes all the requests coming to the server and checks if it is cached, if it already have cached content then it sends back the response to the client else sends it to the web server and stores its response and send the response back to the client. It acts as a cache and HTTP accelerator both because it caches content and also saves server from taking all the requests coming from the clients.

    Understand with an example

    Lets understand varnish cache with an example suppose you are a government officer and you have to answer people questions all day but you are a valuable resource there are other responsibilities on you as well but your full day you get very little time to do productive things, so you have hired someone to give answers to people questions, you gave him a set of questions and answers and after that any question comes from that set of questions you don’t have to answer that your representative will handle that, he will only call you when there will be a question that is not present in the set of questions and he will take your answer and note it down in the set of questions that way you will be free for doing productive things only and all the redundant queries will be handled by the representative.

    So to understand how varnish works just remove government officer with web server and representative with varnish and questions, answers with request and response and set of question and answers with storage.

    Why Magento 2 Need To Use Varnish:

    Magento 2 is a heavy system it takes a lot of resources to actually run it, we can use Magento2 full page cache it will also work but the server will still face storage and load issues, varnish is a saviour it speeds up Magento 2 and also saves lot of resources.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Let’s understand how to setup varnish in Magento 2:

    Install Varnish

    You can install varnish by simply following the steps.

    Configure Varnish with Web Server

    Varnish will be taking all the HTTP requests, so we need to configure varnish to run on port 80(default HTTP port) and need to change web server port to any other port for example 8080.

    In Apache change the LISTEN port to 8080 in Apache configuration file if you are using Ubuntu it will be :


    And also change <VirtualHost *:80> to <VirtualHost *:8080>

    If varnish is installed, open its configuration file, which will be located at :


    and change default backend to communicate with Apache :

    vcl 4.1; 
    backend default 
    	.host = ""; 
    	.port = "8080"; 

    Now simply restart Apache and varnish :

    sudo systemctl restart apache2 varnish

    Now all set you have successfully configured varnish with Apache web server.

    Configure Magento2 For Varnish

    In Magento 2 goto Store > Configuration > System > Full Page Cache

    You will see this configuration form:

    Lets understand all these fields:

    Caching Application: You need to select Varnish here

    TTL for public content: this is the time for which a public cache is preserved after that cache content will be refreshed, public cache means home page, product page, category pages, cms pages

    Access list: here we can provide list of comma(,) separated IP

    Backend host: it is the ip where web server is running which is equivalent to the varnish backend host in our case it is localhost or

    Backend Port: It is the port on which web server is running, which is varnish backend port in our case it is 8080

    Grace period: grace period comes into action when web server is not responding in that case varnish will server upto grace period old cached copy of the page

    After configuring above details we can export the varnish configuration file according to varnish version by clicking on the buttons, it will download a VCL(varnish configuration language) file that we need to rename to :

    default.vcl and replace it with :


    Now again restart the server and varnish :

    sudo systemctl restart apache2 varnish

    Now all set you have successfully configured varnish with Magento 2, the vcl file is a set of rules that how Magento 2 pages will be cached and what pages will not be cached for example account pages and checkout pages are not cached in Magento 2 so if you will not update exported VCL file into varnish configuration then varnish will start caching all the pages.

    We will fully understand VCL file and how to create our own rules in our next blog on varnish.

    One article can’t be enough to discuss all the concepts, will bring more articles related to varnish and Magento2 cache stay tuned.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home