Reading Time: 2 minutes

Varnish Cache is an open source web application accelerator (also known as HTTP accelerator or HTTP reverse proxy caching). Varnish stores (or caches) files or file fragments in memory; this allows Varnish to reduce response time and network bandwidth consumption on future equivalent requests. Unlike web servers like Apache and nginx, Varnish was designed to be used exclusively with the HTTP protocol.

Magento 2.4.2 is tested with Varnish 6.4. Magento 2.4.x is compatible with Varnish 6.x
Magento itself strongly recommends using Varnish in production. Built in full page caching (in the file system or database) is much slower than Varnish and Varnish is designed to speed up HTTP traffic.

You can find more information in the link below:

Varnish Topology Diagram
The following figure shows you a basic view of Varnish in Magento topology.

In the figure above, you can see how HTTP requests from users on the Internet result in numerous requests for CSS, HTML, JavaScript, and images (collectively referred to as resources). Varnish stands in front of the web server and forwards these requests to the web server.

When the web server returns the resources, the cacheable resources are stored in Varnish. Any subsequent requests for these resources are satisfied by Varnish himself (which means that the requests do not reach the web server). Varnish then returns the cached content extremely quickly. The results are faster response times to return content to users and a reduced number of requests that need to be fulfilled by Magento .

Resources cached by Varnish expire at a configurable interval or are replaced by newer versions of the same resources. You can also clear the cache manually using Magento Admin or the magento cache: clean command .

Process overview

In this section we will see how to initially install Varnish with a minimum set of parameters and test its operation. Then you will export a Varnish configuration from the Magento admin and test it again.

The process can be summarized as follows:

  • Install Varnish and test it by going to any Magento page to see if you are getting HTTP response headers indicating that Varnish is working.
  • Install the Magento software and use the Magento Admin to create a Varnish configuration file.
  • Replace your existing Varnish configuration file with the one generated by the administrator.
  • Try everything again.

If there is nothing in your <magento_root> / var / page_cache directory, you have successfully configured Varnish with Magento!

Unless otherwise specified, you must enter all the commands discussed above, as a user with root privileges.
This applies to Varnish on CentOS and Apache 2.4. If you are setting up Varnish in a different environment, some commands may be different. Consult the Varnish documentation for more information.

Problems encountered

In this process some problems can arise, let’s see which ones:

  • Varnish does not support SSL
    Alternatively, use SSL termination or an SSL termination proxy .
  • If you manually delete the contents of the <magento_root> / var / cache directory , you need to restart Varnish.
  • Possible error while installing Magento:
Error 503 Service Unavailable
Service Unavailable
XID: 303394517
Varnish cache server


If this error occurs, edit default.vcl and add a timeout to the backend as follows:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .first_byte_timeout = 600s;
}

pattern-lines

Free 30-days trial Hosting magento Fast, Secure and Optimized

Switch to Bhoost with 30 days free and migration included

Free 30-days trial
macbook