Varnish may be one of the most interesting elements described in this book, besides Magento 2, of course. What is Varnish and why is it that important? Well, Varnish is like a Ferrari, very fast on the track but hard to maintain or tune. In technical terms, Varnish is an HTTP accelerator designed for heavy websites. Magento users love fast websites.
By default, Varnish support is now included in Magento 2. In Magento 1, we commonly used Turpentine by Nexcess (https://github.com/nexcess/magento-turpentine). The configuration of Varnish is not for the faint-hearted. Varnish includes a Varnish Configuration Language (VCL) file, which holds all the elements to be cached or not.
Setting up a Varnish server may be simple; configuring the VCL is not. Magento 2 provides a default VCL file that works out of the box, but be aware of any custom extensions or layout updates. Any customization has to be added manually in the VCL file before Varnish can cache them.
For this recipe, we will use a Droplet created in Chapter 2, Magento 2 System Tools, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX, PHP-FPM, and a Composer-based setup including sample data connected to a Varnish server. No other prerequisites are required.
For the purpose of this recipe, let's assume that we need to create a Magento 2 Varnish setup. The following steps will guide you through this:
apt-get install –y apt-transport-https
By default, all current Ubuntu versions support apt-transport-https.
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" | sudo tee -a /etc/apt/sources.list.d/varnish-cache.list
curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -
apt-get update && apt-get install -y varnish service varnish start
varnishd –V service varnish status netstat –anp | grep varnish
As you can see, the Varnish server is running under ports 6081 and 6082.
sed -i 's/80/8080/' /etc/nginx/conf.d/default.conf
We are using port 8080 as an internal port.
Your configuration file could look as follows:
server { listen 8080; server_name yourdomain.com;
6081
to 80
. Use the following command to change the /etc/default/varnish
file:sed -i 's/6081/80/' /etc/default/varnish
systemd
setup. The systemd
servers will not update their configuration script after a restart or reboot. Let's update this manually using the following command:sed -i 's/6081/80/' /lib/systemd/system/varnish.service
Update the systemd
process with the following code:
systemctl daemon-reload
service varnish restart && service nginx restart
netstat -upnlt | egrep 'varnish|nginx'
Always make sure that Magento is running in the developer mode when setting up Varnish. When ready to launch, we can switch to the production mode:
/etc/varnish/default.vcl
file. Now open the file and change backend default
to the following:backend default { .host = "127.0.0.1"; .port = "8080"; }
service varnish restart php bin/magento cache:clean php bin/magento cache:flush
curl -I http://yourdomain.com
The output of this command should be as follows:
root@mage2cookbook:~# curl -I http://mage2cookbook.com HTTP/1.1 200 OK Date: Mon, 14 Dec 2015 19:15:40 GMT Content-Type: text/html; charset=UTF-8 X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400 Pragma: no-cache Expires: -1 Cache-Control: no-store, no-cache, must-revalidate, max-age=0 Vary: Accept-Encoding Age: 1127 X-Magento-Cache-Debug: HIT Accept-Ranges: bytes Connection: keep-alive
Let's recap and find out what we did throughout this recipe. In steps 1 through 16, we installed and configured Varnish to speed up the full page caching.
In steps 1 through 4, we added the official repository to our system and installed Varnish.
In steps 6 and 7, we changed the NGINX port to 8080 instead of 80, and the Varnish port to 80. Now, Varnish will be our gatekeeper after restarting the NGINX and Varnish servers.
In step 12, we told Magento to start communicating with the Varnish server so that all frontend cacheable data is stored here.
The current lifetime of the cache is 86,400 seconds, which is one day. So, installing a cache warmer will speed up your pages after an automatic cache flush by Magento. Always keep in mind that by default, all the pages are cold (without a cache hit) and the first GET (page view) can take longer. Varnish needs to build up the cache before customers can benefit from it.
Check out the following Varnish tools to monitor all the incoming data live:
varnishstat
The output of this command will be as follows:
Now, let's execute the following command:
Varnishlog
18.220.88.62