How to install Varnish on your Magento 2 site
Nowadays, page speed is a very important aspect of Search Engine Optimization (SEO). On PageSpeed Insights people are able to check their own site speed. Magento 2 is a large software library. It runs on PHP and pages are rendered slower than Javascript-driven software libraries, like React. The results of the PageSpeed scores are often poor. A good way to improve the scores is to implement a cache proxy. Magento 2 has an optimized configuration for the Varnish caching solution.
Note: This tutorial assumes that you have a working Magento 2 installation running on a web server. Furthermore, you should have HTTPS configured for your website.
Installation
sudo apt-get update sudo apt-get install varnish
Configuration
First start of by changing your HTTPS configuration to forward the request to Varnish.
The nginx configurations are as followed:
location / { proxy_pass http://127.0.0.1:6081; proxy_read_timeout 90; proxy_connect_timeout 90; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Port 443; proxy_set_header Host $host; }
Nginx receives an incoming connection on port 443 and forwards it to the Varnish service running on port number 6081. Now you have to set up Varnish in order to cache your Magento files.
Now you have to change the default Varnish configuration file. This file is located at: /etc/varnish/default.vcl. Locate back end default and change the values to:
backend default { .host = "127.0.0.1"; .port = "8080"; }
Last you need to configure your webserver so that it listens to port number 8080. The webserver configuration on this port serves your normal web content. The configuration can be seen below.
server { set $MAGE_ROOT /var/www/; include /var/www/ /nginx.conf.sample; server_name ; error_log /var/log/nginx/ _error.log; access_log /var/log/nginx/ _access.log; listen 8080; }
Restart your both your web-server and your Varnish service.
sudo service nginx restart sudo service varnish restart
Congratulations, you now have successfully installed Varnish. To optimize even further, you can change your Varnish configuration file with the one created by Magento. To get this configuration file, you have to log into your admin dashboard.
- Go to Stores > Settings > Configuration > Advanced > System > Full Page Cache
- Navigate to the Caching Application and select Varnish Caching
- Expand Varnish Configuration and fill in the following information:
- Access list: localhost
- Backend host: 127.0.0.1
- Backend port: 8080
Now save the configuration and export the VCL for your Varnish version. Make a backup of /etc/varnish/default.vcl and copy the output of the export into the default.vcl.
Search for the backend default in the configuration file. Change .url = “/pub/health_check.php”; to .url = “/health_check.php”;. This change will make sure Varnish runs correctly
Restart Varnish, all should be working fine now.
Try to load some web pages and experience an optimized webserver. Now the score in PageSpeed Insights should be higher than before.