Varnish Cache is an open-source web application accelerator (also known as HTTP accelerator or HTTP reverse proxy caching). To speed up subsequent requests, Varnish temporarily stores website elements (files or fragments) in memory. This focus on in-memory caching for the HTTP protocol allows Varnish to significantly reduce response times and network traffic. This allows Varnish in Magento 2 to significantly reduce response time and network bandwidth consumption on future equivalent requests.
Magento 2.4.2 has been specifically tested for compatibility with Varnish 6.4. While all versions of Magento 2.4 are compatible with Varnish 6.x, Magento strongly recommends using Varnish in a production environment. Varnish, designed to accelerate HTTP traffic, significantly outperforms Magento’s built-in full-page caching stored in the file system or database.
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.
Upon receiving a request, the web server identifies cacheable resources. These resources are then stored in Varnish, which fulfils subsequent requests for them directly. This bypasses the web server, improving performance. Varnish then returns the cached content extremely quickly. The benefits include faster response times for content delivery and a lower request load on 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; } |
That’s it, folks! Check out our blog for more information.
Free 30-days trial Hosting magento Fast, Secure and Optimized
Switch to Bhoost with 30 days free and migration included
Free 30-days trial