How to install varnish cache with Nginx webserver

In this article, we are going to deploy a nginx server and a varnish cache
We have already learned how to install Nginx webserver and deploy a website in it in our previous articles How to install Nginx in Ubuntu 20.04.

Varnish Cache is an HTTP accelerator designed to significantly improve the performance of web servers. It works by storing a copy of the requested web pages in memory, allowing subsequent requests to be served directly from the cache rather than processing them from the web server. This reduces server load, accelerates content delivery, and enhances overall website responsiveness. Varnish Cache is particularly effective in handling high-traffic websites, making it a valuable tool for optimizing web server performance and ensuring a faster, more efficient user experience.

Here we are going to install and deploy the nginx webserver on port 8080 and the varnish cache on port 80 so any request coming to port 80 on the server will be cached by the varnish cache and forwarded to the nginx webserver which reduces the loading time.

In this article, I am using the Ubuntu server. You must need a Ubuntu server with root access.
SSH into the server

Step 1: Update the repo and install the varnish cache.

First of all, we need to update our apt repo using the apt-get update -y command.

After the installation, you can check the status of the varnish cache using the command.

You can also verify the installed version using the command.

Step 2: Installing the nginx webserver

By default, nginx will run on port 80 so we need to change the listening port to 8080.

This will install the nginx webserver and on the /etc/nginx/sites-available/default conf we need to change the listening port to 8080 as shown below.

After that, we need to restart the nginx webserver.

Step 3: Changing the port of the varnish service

By default varnish will be listening on ports 6081 and 6082 you can check this by using the netstat command.

You need to edit the systemd file on the location /lib/systemd/system/varnish.service
and change the default port from 6081 to 80 in the line ExecStart as shown below.

Next, we need to reload the daemon using the command systemctl daemon-reload.

Then you need to configure nginx as a backend webserver for varnish you can do this by editing the conf file at the location /etc/varnish/defualt.vcl

Here we don’t need to make any changes as our nginx is already running on port 8080 if you are running nginx on another port make changes to this file

After that restart the varnish service using the command

Step 4: Verify the varnish service.

Now any request coming to port 80 will be severed/cached by varnish and forwarded back to nginx test this using the curl command.

You will be able to see a result as shown below which means nginx is serving with a varnish cache.

This configuration is a simple one but you can use the same concept for serving varnish cache to your own websites via nginx and you can use SSL certificates also.
In this article, we have learned how to install and configure the varnish cache along with the nginx webserver

Leave a Reply

Your email address will not be published. Required fields are marked *