Install and Configure Varnish on AlmaLinux 9

By Raman Kumar

Updated on Oct 10, 2024

In this tutorial, we'll learn how to install and configure Varnish on AlmaLinux 9. 

Introduction

Varnish is a powerful HTTP accelerator designed to cache web pages, reducing load times and improving performance for dynamic, content-heavy websites. By storing copies of frequently accessed content in memory, Varnish helps offload the web server, reducing its resource consumption and significantly enhancing page speed.

This tutorial will guide you through installing and configuring Varnish on AlmaLinux 9, and will demonstrate how to configure Varnish to work with a web server, such as Nginx or Apache.

Prerequisites

  • A server running AlmaLinux 9 dedicated server or KVM VPS.
  • Root or a user with sudo privileges.
  • Nginx or Apache web server running (This tutorial assumes Nginx, but the steps are similar for Apache).

Install and Configure Varnish on AlmaLinux 9

Step 1: Install Varnish on AlmaLinux 9

Varnish is available in the default AlmaLinux repositories, so you can install it directly using dnf.

1.1 Update your system packages

Before installing any software, it's always a good practice to update your system packages.

sudo dnf update -y

1.2 Install Varnish

Install Varnish using the dnf package manager.

sudo dnf install varnish -y

1.3 Enable and start Varnish

Once Varnish is installed, enable it to start on boot and start the service.

sudo systemctl enable --now varnish
sudo systemctl start varnish

You can check the status of Varnish to ensure it's running correctly.

sudo systemctl status varnish

Step 2: Configure Varnish

Varnish listens on port 6081 by default. To configure it to serve content on port 80, we need to modify its default configuration.

2.1 Edit the Varnish configuration file

Open the Varnish service file using a text editor to modify the listening port.

sudo vi /usr/lib/systemd/system/varnish.service

Look for the line that starts with ExecStart and change the port from 6081 to 80. It should look like this:

ExecStart=/usr/sbin/varnishd \
    -a :80 \
    -T localhost:6082 \
    -S /etc/varnish/secret \
    -s malloc,256m

Here, Varnish will listen on port 80 and allocate 256MB of memory for caching (-s malloc,256m).

Save and close the file after editing.

2.2 Reload the systemd daemon and restart Varnish

After editing the service file, reload the systemd daemon to apply the changes and restart the Varnish service.

sudo systemctl daemon-reload
sudo systemctl restart varnish

Step 3: Configure Nginx to Work with Varnish

Now, you need to configure Nginx to work with Varnish by listening on port 8080 instead of 80. Varnish will act as a reverse proxy, serving cached content from Nginx.

3.1 Edit the Nginx configuration

Open your Nginx configuration file.

sudo vi /etc/nginx/nginx.conf

Find the server block, and modify the listen directive to port 8080:

server {
    listen 8080;
    server_name your_domain_or_IP;

    location / {
        proxy_pass http://127.0.0.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Save and close the file.

3.2 Restart Nginx

Restart Nginx for the changes to take effect.

sudo systemctl restart nginx

Step 4: Test the Varnish Cache

Now that Varnish is configured as a reverse proxy in front of Nginx, you can test if Varnish is caching content properly.

4.1 Verify Varnish is working

Use the curl command to see the headers and check if Varnish is serving cached content.

curl -I http://your_domain_or_IP

Look for the X-Varnish header. If Varnish is working correctly, you should see something like this in the output:

HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 10 Oct 2024 12:34:56 GMT
X-Varnish: 32775
Age: 0
Via: 1.1 varnish (Varnish/6.0)

The Age header shows how long the content has been cached in seconds. If the Age is 0, that means the content is being fetched from the backend server (Nginx). If you refresh the page and see the Age value increase, Varnish is caching the page.

Step 5: Fine-Tuning Varnish Configuration

Varnish uses the Varnish Configuration Language (VCL) to define its behavior. The default VCL file is located at /etc/varnish/default.vcl.

5.1 Edit the VCL file

Open the default VCL file:

sudo vi /etc/varnish/default.vcl

In this file, you can define backend servers, set caching rules, and customize how Varnish handles requests.

By default, Varnish passes requests to the backend (Nginx) as shown in the backend default section. You can adjust the backend definition as needed:

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

You can also customize caching policies. For example, to cache all static files for 24 hours, add the following rule under the sub vcl_recv section:

sub vcl_recv {
    if (req.url ~ "\.(jpg|jpeg|png|gif|css|js)$") {
        set req.http.Cache-Control = "max-age=86400";
    }
}

This tells Varnish to cache static files for 24 hours (86400 seconds).

5.2 Restart Varnish

After making changes to the VCL file, restart Varnish to apply the new configuration.

sudo systemctl restart varnish

Step 6: Monitoring Varnish

Varnish provides several built-in tools for monitoring and debugging.

6.1 View Varnish logs

You can monitor real-time logs using varnishlog:

sudo varnishlog

6.2 View cache statistics

Use varnishstat to see detailed statistics about Varnish’s performance and cache hits:

sudo varnishstat

Conclusion

We have seen how to install and configure Varnish on AlmaLinux 9. By setting up Varnish on AlmaLinux 9, you can significantly improve the performance of your web server, reduce load times, and handle more traffic efficiently. With Varnish caching your dynamic content, you can offload your web server and deliver faster page loads to users.

Varnish is highly customizable, so feel free to explore its extensive documentation to further optimize your caching strategy.