Install Varnish Cache with Nginx on Ubuntu 22.04

This tutorial intends to teach you to Install Varnish Cache with Nginx on Ubuntu 22.04.

Varnish cache is a web application accelerator also known as caching HTTP reverse proxy. It acts more like a middleman between your client (i.e. user) and your web server. That means, instead of your web server directly listening to requests for specific content all the time, Varnish will assume the responsibility.

Steps To Install Varnish Cache with Nginx on Ubuntu 22.04

To complete this guide, log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Also, you need to have Nginx installed on your server. To do this, you can follow this guide on How To Install Nginx on Ubuntu 22.04.

Configure Nginx on Ubuntu 22.04

First, update your local package index with the following command:

sudo apt update

The default port of Nginx is 80. You need to change it to 8080.

To do this, you can use the following command:

sudo find /etc/nginx/sites-enabled -name '*.conf' -exec sed -r -i 's/\blisten ([^:]+:)?80\b([^;]*);/listen \18080\2;/g' {} ';'

The above script will edit any config in the path /etc/nginx/sites-enabled 

Also, you may need to edit the default Nginx site to listen on port 8080. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/nginx/sites-enabled/default

Apply the changes as shown below:

......
server {
        listen 8080 default_server;
        #listen [::]:80 default_server;

        # SSL configuration
        #

When you are done, save and close the file.

Now restart Nginx to apply the changes with the following command:

sudo systemctl restart nginx

Then, verify that Nginx is listening on port 8080 with the following command:

sudo netstat -pnlt | grep 8080

In your output you should see:

Output
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3200/nginx: master

Now you can start to install Varnish Cache on Ubuntu 22.04.

Install Varnish on Ubuntu 22.04

To install the Varnish Cache, you need to add the official Varnish Cache repository.

First, add the required dependencies with the following command:

sudo apt install debian-archive-keyring curl gnupg apt-transport-https -y

Add Varnish Cache 7.3 GPG Key

Then, add the GPG key for the package with the following curl command:

curl -fsSL https://packagecloud.io/varnishcache/varnish73/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg

Add Varnish Cache 7.3 Repository

Now add the Varnish Cache repository with the command below:

sudo tee /etc/apt/sources.list.d/varnishcache_varnish73.list > /dev/null <<-EOF
deb https://packagecloud.io/varnishcache/varnish73/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish73/ubuntu/ focal main
EOF

Update your local package index:

sudo apt update

Now you can install Varnish on Ubuntu 22.04 with the following command:

sudo apt install varnish -y

Here you have Varnish Cache installed on your server. Let’s see how to configure it.

Configure Varnish Cache on Ubuntu 22.04

At this point, you need to check the default address and port configuration. Open the Varnish configuration file with your favorite text editor, here we use vi:

sudo vi /etc/varnish/default.vcl

On the “backend default” section be sure that it looks like this:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

When you are done, save and close the file.

Now you need to configure Varnish to listen at port 80 instead of the default of 6081.

To be able to manage Varnish Cache 7 like other system services, we will adjust the systemd service as below.

sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
cat /etc/systemd/system/varnish.service

Then, edit the file and change the default port to port 80 and the cache size to 2GB.

sudo vi /etc/systemd/system/varnish.service
ExecStart=/usr/sbin/varnishd \
	  -a :80 \
	  -a localhost:8443,PROXY \
	  -p feature=+http2 \
	  -f /etc/varnish/default.vcl \
	  -s malloc,2g

Save and close the file, when you are done.

To register the change reload the systemd with the following command:

sudo systemctl daemon-reload

Restart Varnish to apply the changes:

sudo systemctl restart varnish

Verify that Varnish is listening on port 80 with the following command:

sudo netstat -ltnp | grep ':80'

In your output you will see:

Output
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4829/varnishd
tcp6       0      0 :::80                   :::*                    LISTEN      4829/varnishd

Testing Varnish Cache

Now you can use the curl command to test the Varnish Cache on Ubuntu 22.04:

curl -I http://localhost/
Output
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Thu, 16 Mar 2023 12:41:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 16 Mar 2023 12:34:54 GMT
ETag: "64130cee-264"
X-Varnish: 2
Age: 0
Via: 1.1 ubuntu.jammy (Varnish/7.3)
Accept-Ranges: bytes
Connection: keep-alive

Be sure that the X-Varnish: 2 and Via: 1.1 varnish (Varnish/7.3) headers appear in the output.

Conclusion

At this point, you have learned to Install Varnish Cache with Nginx on Ubuntu 22.04.

Hope you enjoy it. For more articles, you can visit the Orcacore Website.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!