What is Varnish
Varnish is a powerful reverse proxy server that is crucial in optimizing web performance by handling all incoming requests before they reach the web server.
In the context of Caching in Magento 2, Varnish first checks if the requested content is already cached.
If it is, Varnish quickly returns the cached response to the client, significantly reducing load times.
If the content is not cached, Varnish forwards the request to the web server, then caches the server’s response for future requests, ensuring faster delivery next time.
Understand with an example
As a government officer with numerous responsibilities, answering questions all day leaves you little time for other productive tasks. To manage this, you’ve hired someone to handle public inquiries.
He will only call you when there will be a question that is not present in the set of questions.
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 varnish 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 Adobe Commerce (Magento 2) Need To Use Varnish:
Adobe Commerce (Magento 2) is a resource-intensive platform that requires considerable resources to operate efficiently.
While enabling full-page caching is one of the essential Magento 2 speed optimization techniques, it may not completely resolve server load and storage challenges.
Implementing Varnish, however, can be a game-changer, as it not only accelerates Adobe Commerce (Magento 2) but also conserves server resources significantly
Let’s understand how to setup varnish in Adobe Commerce (Magento 2):
Install Varnish
Follow the varnish installation process.
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 :
/etc/apache2/ports.conf
And also change <VirtualHost *:80> to <VirtualHost *:8080>
If varnish installed, open its configuration file, which will be located at :
/etc/varnish/default.vcl
and change default backend to communicate with Apache :
vcl 4.1; backend default { .host = "127.0.0.1"; .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 Adobe Commerce (Magento 2) For Varnish
In Adobe Commerce (Magento 2) goto Store > Configuration > System > Full Page Cache
You will see this configuration form:

Let’s 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 127.0.0.1
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 :
/etc/varnish/default.vcl
Now again restart the server and varnish :
sudo systemctl restart apache2 varnish
Now all set you have successfully configured varnish with Adobe Commerce (Magento 2).
The vcl file is a set of rules that how Adobe Commerce (Magento 2) pages will be cached and what pages will not be cached. Will explain by the following example-
Account pages and checkout pages are not cached in Adobe Commerce (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 Adobe Commerce (Magento 2) cache stay tuned.
If you require technical support, feel free to email us at [email protected].
Additionally, explore a wide array of solutions to boost your store’s capabilities by visiting the Adobe Commerce modules section.
For expert advice or to create tailored features, hire Adobe Commerce Developers for your project.
Be the first to comment.