Install and Configure Varnish HTTP on Ubuntu

By Anurag Singh

Updated on Oct 09, 2024

Install and Configure Varnish HTTP on Ubuntu

In this tutorial, we'll learn how to install and configure Varnish HTTP on Ubuntu 24.04 server.

Varnish is a high-performance HTTP accelerator designed to improve the speed of content-heavy dynamic websites. It works by caching web pages in memory, reducing the load on your web server and significantly speeding up the time it takes for users to access your website.

Prequisites:

  • A Ubuntu 24.04 installed dedicated server or KVM VPS.
  • Apache or Nginx installed on the system.
  • Basic Linux command knowledget.

In this tutorial, we will cover:

Installing Varnish on Ubuntu 24.04.
Configuring Varnish to work with an existing web server (Nginx or Apache).
Setting up caching rules for your website.
Testing and verifying that Varnish is caching correctly.

Install and Configure Varnish HTTP on Ubuntu

Step 1: Update Ubuntu System

Before installing any packages, update your system to ensure you have the latest package information.

sudo apt update && sudo apt upgrade -y

Step 2: Installing Varnish

The Varnish package is available in Ubuntu’s default repository. To install it, use the following command:

sudo apt install varnish -y

Once the installation is complete, check the version of Varnish to verify the installation:

varnishd -V

This will show the installed version of Varnish, confirming that the installation was successful.

varnishd (varnish-7.1.1 revision 7cee1c581bead20e88d101ab3d72afb29f14d87a)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2022 Varnish Software

Step 3: Configuring Varnish to Work with Apache or Nginx

Configuring Varnish with Apache

Modify Apache’s port:

By default, Apache runs on port 80, which is the same port Varnish will use to accept requests from users. To resolve this conflict, we need to change Apache’s port to 8080.

Open the Apache configuration file:

sudo nano /etc/apache2/ports.conf

Change the following line from:

Listen 80

To:

Listen 8080

Update the Apache Virtual Host configuration:

Open your Apache virtual host configuration file (usually located in /etc/apache2/sites-available/000-default.conf):

sudo nano /etc/apache2/sites-available/000-default.conf

Change the <VirtualHost> directive to use port 8080 instead of port 80:

<VirtualHost *:8080>

Restart Apache:

After making these changes, restart Apache to apply the changes:

sudo systemctl restart apache2

Configuring Varnish to Serve on Port 80

Update Varnish port configuration:

By default, Varnish listens on port 6081. We need to change this to port 80, which is the standard HTTP port. To do this, open the Varnish service configuration file:

sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service

Find the following line:

ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -s malloc,256m

Change it to:

ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -s malloc,256m

Restart Varnish:

After modifying the configuration file, reload the systemd configuration and restart Varnish to apply the changes:

sudo systemctl daemon-reload
sudo systemctl restart varnish

Now, Varnish will serve requests on port 80, and Apache will be running on port 8080.

Configuring Varnish with Nginx

If you're using Nginx as your web server, the configuration steps are very similar to Apache.

Modify Nginx’s port:

Open the Nginx configuration file located in /etc/nginx/sites-available/default:

sudo nano /etc/nginx/sites-available/default

Change the listen directive to use port 8080:

listen 8080 default_server;

Restart Nginx:

After making the changes, restart Nginx:

sudo systemctl restart nginx

Configure Varnish to listen on port 80:

Similar to the Apache setup, modify the Varnish configuration as shown in the Apache section above, changing Varnish’s listening port to 80.

Step 4: Configuring Varnish for Caching

Varnish’s configuration file is called the Varnish Configuration Language (VCL) file, which controls how Varnish behaves when it receives requests. The default VCL file is located at /etc/varnish/default.vcl.

Open the VCL file:

sudo nano /etc/varnish/default.vcl

Basic Backend Configuration

Here, you can define your backend server (Apache or Nginx) that Varnish will cache content from. By default, Varnish will send requests to localhost on port 8080, which we set up earlier.

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

Caching Static Content

You can configure Varnish to cache static assets such as CSS, JavaScript, and images. Add this rule to the vcl_recv function:

sub vcl_recv {
    if (req.url ~ "\.(png|jpg|jpeg|gif|css|js)$") {
        return (hash);
    }
}

This will tell Varnish to cache static files.

Handling Cookies

You may want to prevent Varnish from caching pages for logged-in users (or users who have cookies set). You can do this by modifying the vcl_recv function as follows:

sub vcl_recv {
    if (req.http.Cookie) {
        return (pass);
    }
}

This tells Varnish to bypass the cache if a cookie is present.

Step 5: Testing Varnish Cache

To check if Varnish is caching properly, you can use the curl command. Make a request to your website and look for the X-Cache header:

curl -I http://your-domain.com

You should see something like this:

HTTP/1.1 200 OK
X-Cache: MISS

This means the request did not come from the cache (because it was the first request). If you run the command again, you should see:

HTTP/1.1 200 OK
X-Cache: HIT
X-Varnish: 2

This indicates that the page was served from Varnish’s cache.

Step 6: Monitoring Varnish

You can monitor Varnish’s performance using the varnishstat command, which provides real-time statistics about cache hits, misses, and other performance metrics.

varnishstat

This will give you detailed information on how Varnish is handling requests.

Conclusion

In this tutorial, we have seen how to install and configure Varnish HTTP on Ubuntu 24.04 server, and integrated it with a web server (Apache or Nginx). Varnish significantly improves the speed and performance of dynamic content-heavy websites by caching web pages in memory and reducing load times for end-users. With proper configuration, Varnish can be a powerful tool to boost your website's performance.