Configuring Varnish for Magento
Magento is one of the most popular e-commerce platforms out there and has both a Community Edition and an Enterprise Edition.
Magento is very flexible and versatile and that comes at a cost: the performance of a Magento store with lots of products and lots of visitors is often quite poor without a decent caching layer.
Luckily Magento has built-in caching mechanisms and the full page cache system has native support for Varnish.
This tutorial is a step-by-step guide on how to configure Varnish for Magento.
This tutorial features Magento 2.4 Community Edition and Varnish 9.
1. Install and configure Varnish#
If you already have your Magento store up-and-running, and you’re looking to use Varnish as a full-page cache, you’ll have to decide where to install Varnish:
- You can install Varnish on a dedicated machine and point your DNS records to that server.
- You can install Varnish on the same server as your Magento store.
In this tutorial we’re installing Varnish on the same server where the Magento store is located.
For a detailed step-by-step Varnish installation guide, we’d like to refer you to the install guide.
2. Reconfigure the web server port#
The web server that is hosting your Magento store is most likely set up to handle incoming HTTP requests on port 80 and HTTPS requests on port 443. For Varnish caching to properly work, Varnish needs to listen on ports 80 and 443. This also means that your web server needs to be configured on another listening port. We’ll use port 8080 as the new web server listening port for plain HTTP and port 8443 for HTTPS.
Depending on the type of web server you’re using, different configuration files need to be modified. The following tutorial explains how to change the web server listening ports.
3. Enable Varnish Full-Page Cache in the Magento admin panel#
To ensure that Magento is aware of Varnish, you need to enable Varnish as a caching application in Magento’s Full Page Cache configuration.
Here’s how to configure this:
- Go to the Magento admin panel
- Select
Stores>Configuration>Advanced>System - Expand the
Full Page Cachesection - Select
Varnish Cacheas the Caching Application - Optionally modify the
TTL for public contentvalue to override the standard Time-To-Live - Expand the
Varnish Configurationsection - Optionally modify the
Access listvalue to override the list of allowed hosts to purge the cache - Optionally modify the
Backend hostvalue to override the hostname of your Varnish backend - Optionally modify the
Backend portvalue to override the port number of your Varnish backend - Optionally modify the
Grace periodvalue to override the amount of grace time Varnish applies - To confirm the changes, press the
Save Configbutton - Click
Export VCL for Varnish 6to download to custom-generated VCL file.
In this tutorial we’re assuming that Varnish and Magento are hosted on the same server. This means the following default values can be used:
- The
Access listvalue can be set tolocalhost - The
Backend hostvalue can be set tolocalhost - The
Backend portvalue can be set to8080
Other values, such as TTL for public content and Grace period, can use their respective default values: 86400 for the TTL and 300 for the Grace period.
4. Deploy the custom Magento VCL#
In the previous step we downloaded a custom-generated VCL. Please ensure that this file is deployed to /etc/varnish/default.vcl on your Varnish server.
An alternative solution is to run the following command:
sudo bin/magento varnish:vcl:generate --export-version=6 | sudo tee /etc/varnish/default.vcl > /dev/null
This command will regenerate the VCL file using Magento’s command line interface and export it using the Varnish 6 syntax. The output will be written to /etc/varnish/default.vcl, which is the standard VCL file.
Fixing the backend health checks for Magento 2.4#
If you’re using Magento 2.4, your web server’s root folder will be configured to the location of your Magento pub folder.
When your Magento root folder would be /var/www/html, the web server’s root will be /var/www/html/pub. In older versions of Magento that was not the case and /var/www/html would have been the root folder.
The problem is that Magento’s generated VCL file still points to the /pub/health_check.php endpoint, as you can see in the VCL snippet below:
backend default {
.host = "localhost";
.port = "8080";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
}
The probe that performs the health checks will return HTTP 404 errors and as a consequence Varnish will return HTTP 503 Backend fetch failed errors because it considers the backend to be unhealthy.
To fix this, change the .url property for the probe from /pub/health_check.php to /health_check.php as illustrated below:
backend default {
.host = "localhost";
.port = "8080";
.first_byte_timeout = 600s;
.probe = {
.url = "/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
}
You can also run the following command to perform a find and replace for you:
sudo sed -i "s/\/pub\/health_check.php/\/health_check.php/g" /etc/varnish/default.vcl
If you’re using an older version of Magento 2 that has the web root outside of the pub folder, you can ignore this fix.
Optimized Magento VCL file#
Although the generated VCL file is pretty decent once the health check is fixed, there are still some optimizations that can be made.
This VCL file uses the xkey VMOD that is not packaged in older versions of Varnish. As of Varnish 9 XKey is fully packaged, allowing you to perform tag-based cache invalidation.
If you’re using an older version of Varnish, you can either download the source compile XKey yourself, our you can swap out XKey and use bans instead.
