Introduction

Varnish is a web application accelerator that can increase the speed of a website, while simultaneously reducing the load on the Web server. Varnish Cache is a free, open-source, and powerful web application accelerator for dynamic websites with high loads.

By using Varnish Cache behind your web server, your website can be speeded up by a factor between 300 and 1000. Additionally, you can use it as a load balancer if you are running multiple servers. Varnish Cache works by caching requested web pages in memory and serving a requested page without the delay of building content from scratch when the same information is asked for several times.

Configuration

In this tutorial, we will show you how to set up Varnish Cache as a proxy server for Nginx on Ubuntu 20.04.

Make sure that the packages in your system are up-to-date by updating them first. Run the following command as root on your server:

apt-get update -y

You can install varnish using the following command:

apt-get install varnish -y

You can check the status of Varnish service with the following command:

systemctl status varnish

You can also verify the installed version of Varnish with the following command:

varnishd -V

You can verify listening port of Varnish cache with the following command:

netstat -ant

Install and Configure Nginx

First, install the Nginx web server with the following command:

apt-get install nginx -y

After installing the Nginx web server, edit the Nginx default virtual host configuration file:

vi /etc/nginx/sites-available/default

Change the default port from 80 to 8088, as shown below:

server {

listen 8088 default_server;

listen [::]:8088 default_server;

root /var/www/html;

index index.html index.htm index.nginx-debian.html;

server_name _;

location / {

try_files $uri $uri/ =404;

}

}

Note : Ensure you have deleted or commented out all of the default entries in the files, make sure only the above content is saved. Failure to do this will result in the nginx service refusing to start.

Save and close the file when you are finished. Then, restart the Nginx service to apply the configuration:

systemctl restart nginx

Step 4 – Configure Varnish Cache

Next, you will need to configure Varnish to use port 80 so it can route traffic to the Nginx web server via the Varnish cache server.

You can do it by editing the file /lib/systemd/system/varnish.service: Change the Varnish default port from 6081 to 80 as shown below:

vi /lib/systemd/system/varnish.service

Change the Varnish default port from 6081 to 80 as shown below: [Unit]
Description=Varnish HTTP accelerator Documentation=https://www.varnish-cache.org/docs/6.1/ man:varnishd

[Service]

Type=simple

LimitNOFILE=131072

LimitMEMLOCK=82000

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s mall

oc,256m ExecReload=/usr/share/varnish/varnishreload ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install] WantedBy=multi-user.target

Save and close the file when you are finished. Then, reload systemd daemon with the following command:

systemctl daemon-reload

Next, you will need to configure Nginx as a backend server for Varnish. You can do it by editing the file /etc/varnish/default.vcl:

vi /etc/varnish/default.vcl

Change the port from 8080 to 8088, as shown below:

}
Save and close the file when you are finished.

Note: Replace 127.0.0.1 with your Nginx server IP address if your Nginx web server is installed on the other host.

Finally, restart the Varnish cache server with the following command:

systemctl restart varnish

Step 5 – Verify Varnish Cache Server

At this point, the Varnish cache is configured to work with the Nginx web server. It’s time to test it.

You can check the Varnish cache with the curl command as shown below: You can also verify Varnish caching statistics with the following command:

Varnishstat

You should see the following screen:

backend default {

.host = “127.0.0.1”;

.port = “8088”;

You can also see Varnish log entry ranking with the following command:

varnishtop

If you have any issues with the Varnish cache, you can check the Varnish log with the following commands:

tail -f varnishncsa.log

Conclusion

Your website should now load faster because Varnish will retrieve most of the frequently requested content from the memory.Enjoy the enhanced performance, thanks to the blazing fast Varnish cache.