Magento 2 Varnish Configuration: Complete Step-by-Step Guide
What is Varnish Cache?
It is the open-source application accelerator. Varnish stores file fragments or files in memory, which enable this accelerator to reduce the overall network bandwidth intake and response time on upcoming, equivalent requests. The unique thing about varnish is that it is suitable to use with HTTP protocol.
Why opt for a Varnish cache?
Almost each web service generally receives the HTTP requests as well as returns the HTTP responses. Unluckily, it never returns the desired response immediately.
There is an extensive range of steps required for every request. It is because they are loading the script files, performing the database queries, and launching the interpreter processes. When every action is multiplied by several requests, then the server gets overloaded.
Also, system resources are fully drained. In this stage, you can see a considerable performance decrease. If you want to increase the performance, you can opt for magento 2 varnish vcl.
It is a specially developed file that supports several stores better and permits purging the arbitrary URLs.
When you are using the varnish cache server, you can ensure that the requests are received by this accelerator rather than a web server. In simple words, the reliable accelerator can take care of the entire responsibility while letting your server to perform the standard tasks.
If you desire to grab these benefits, you can immediately perform magento 2 varnish cache setup.
-
How to configure Magento for using Varnish
When you decide to use the magento 2 configure varnish cache, it is advised to follow these steps.
- The initial step is to log in to Magento Admin as the administrator.
- After that, you can click on Stores.
- This process continues to Settings > Configuration > ADVANCED > System > Full Page Cache.
- You can find out the Vanish Caching option from the list of Caching Application.
- Once you have to find Vanish Caching, you can click on it.
- Here, you can enter the value in TTL for the public content field.
- Expand Varnish Configuration.
- Click on save the configuration.
Steps involved in installing the varnish cache
If you want to Install varnish on server, you can look at the following steps.
- At first, you can install the varnish cache and examine it through Magento page by using the command known as sudo apt-get install varnish.
- From the admin panel of Magento, you can go to the Stores > Configuration > General > Web > Base URLs (Secure). In this step, you need to modify Offloader header to exactly X-Forwarded-Proto from SSL_OFFLOADED before saving the configuration.
- The next step is to visit Stores -> Configuration -> Advanced -> System -> Full Page Cache from the admin panel. After that, you can change a Caching Application to the chosen Varnish Cache from Built-in Cache.
Here, you can uncheck the TTL value for the public content and also set default value as 86400.
You can modify the overall access list and also backend host to 127.0.0.1 from the localhost.
It is done from the Varnish configuration tab. After that, you can set a backend port as 8080 before saving the configuration.
If you want to flush varnish cache, you can enter php bin/magento cache:flush to flush the magento cache.
Once you have saved the configuration, you can enter Export Configuration from
Stores -> Configuration -> Advanced -> System -> Full Page Cache -> Varnish Configuration tab,
click on the Export VCL for Varnish.It will download the
varnish.vcl
file and export it to/var/www/html/magento/var/
directory.After that, you can login to the ssh by using root user. Then rename the file from
/var/www/html/magento/var/varnish.vcl to /var/www/html/magento/var/default.vcl
Then, you can go to varnish location by enter cd
/etc/varnish/
Here you can take a backup of your original
default.vcl
file and place the default.vcl file that is actually exported from magento.-
By opening
/etc/default/varnish,
you can locate forDAEMON_OPTS to change the port to -a :80 from -a :6081 DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -p thread_pool_min=1 \ -p thread_pool_max=1500 \ -p http_resp_hdr_len=65536 \ -p http_resp_size=98304 \ -S /etc/varnish/secret \ -s malloc,768m"
-
For changing the varnish port exactly from 6081 to 80, you can edit the system service configuration as follows:
sudo nano /etc/systemd/system/multiuser.target.wants/varnish.service
It will open a file where you can look at the lineExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 –T 127.0.0.1:6082 -f/etc/varnish/default.vcl -S /etc/varnish/secrets malloc,256m
In this step, you can comment it by using a hash symbol#ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081-T 127.0.0.1:6082 -f/etc/varnish/default.vcl -S/etc/varnish/secret -s malloc,256m
After that, you can add a line which is given below:ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T127.0.0.1:6082 -f/etc/varnish/default.vcl -S /etc/varnish/secret-s malloc,1536m
-
You can now change the Apache listening port from 80 to 8080. If you want to perform this task properly, you can open Apache ports configuration file. You can change it with this command.
nano /etc/apache2/ports.conf
Listen 80 -> Listen 8080
nano /etc/apache2/sites-available/magento.conf<VirtualHost *:80> -> <VirtualHost *:8080>
-
It is the right stage to run this commands to restart apache and varnish service for checking their status
sudo systemctl daemon-reload sudo service apache2 restart sudo service apache2 status sudo service varnish restart sudo service varnish status
-
Then, you can examine the ports by enter the command given below:
sudo netstat -ltnp | grep :80
Output of your command should be that the apache should be on the 8080 port and varnish on the 80 port.
Guide on exporting the Varnish configuration file
This process can be done from the admin panel.
- You can click on the export buttons for creating magento 2 varnish default.vcl that you utilize with the Varnish cache
- For example, if you prefer Varnish 4, you can click on Export VCL for Varnish 4
- You can back up the existing default.vcl and rename your varnish.vcl file you just exported to default.vcl.
-
After that, you can copy a file to
/etc/varnish/. directory. $ cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak2 $ mv <download_directory>/varnish.vcl default.vcl $ cp <download_directory>/default.vcl /etc/varnish/default.vcl
-
Open default.vcl and modify the value of your acl purge to an IP address of Varnish host.
acl purge { "localhost"; }
- For an additional option, you can opt for Advanced Varnish configuration.
-
Finally, you can restart both the Varnish and web server
$ service varnish restart $ service httpd restart
How to verify the working of varnish cache?
If you want to know whether varnish cache is functioning of not, you can examine it by following this command:
curl -I -v --location-trusted 'http://127.0.0.1/magento233/' You can ensure that magento is in a developer mode for debugging about headers. According to below headers, one can recognize that varnish is caching a page or not X-Magento-Cache-Control: max-age=86400, public, smaxage=86400 Age:1381 X-Magento-Cache-Debug: HIT
-
How can cache works and its benefits?
f you want to know how cache works, you can read this passage properly. The varnish http cache can work under a common principle.
It is the task of storing the data in the temporary storage unit, known as a cache. When you use this accelerator, you can acquire several benefits, including:
- The overall load on the back end is reduced
- The response time is improved
- The system will serve many requests
There are lots of benefits associated with varnish reverse proxy that includes Security, SSL download or SSL termination, GZIP compression, and Centralized administration of several SSL certificates.
Conclusion:
If you want to boost the speed of the store page, you can focus on Magento 2 Vanish cash. It ensures that the loading speed of your site is increased roughly several times.
If you have any doubts about installing this cache, you can read the above guidelines properly. It is highly advised to consider the varnish version to enjoy the latest features and other benefits of this Magento 2 extension.