Install Varnish Cache with Nginx on AlmaLinux 9 | Best Guide

This guide intends to teach you How To Install Varnish Cache with Nginx on AlmaLinux 9. 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.

You can now proceed to the steps below on the Orcacore website to set up the Nginx Varnish Cache on AlmaLinux 9.

Steps To Install Varnish Cache with Nginx on AlmaLinux 9

To set up the Nginx Varnish Cache on AlmaLinux 9, you must 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 this guide on Initial Server Setup with AlmaLinux 9.

Also, you need Nginx installed on your server. You can visit the installation part of this guide on How To Install Nginx on AlmaLinux 9.

Now follow the steps below to complete the Nginx Varnish Cache on AlmaLinux 9.

1. Install Varnish Cache on AlmaLinux 9

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

sudo dnf -y update

Then, disable any existing varnish modules on your server with the below command:

sudo dnf module disable varnish

Next, install the Epel release package by typing:

# . /etc/os-release 
# sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-${VERSION_ID%%.*}.noarch.rpm

At this point, you need to visit the Varnish Downloads page, check for the latest release, and add the Varnish Cache repository to your system:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish72/script.rpm.sh | sudo bash

When your download is completed, you will get the following output:

Output
The repository is setup! You can now install packages.

Install the package with the following command:

sudo dnf install varnish -y

Manage Varnish Cache Service

Start and enable the Varnish service to start on boot:

sudo systemctl start varnish
sudo systemctl enable varnish

Verify that your Varnish Cache is active and running on AlmaLinux 9:

sudo systemctl status varnish
Varnish Cache is active and running on AlmaLinux 9

2. Configure Varnish on AlmaLinux 9

The default Varnish listening port is 6081. We need to change the default port to 80 since Varnish sits in front of the web server and accepts incoming HTTP connections.

To do this, open the Varnishd file with your favorite text editor, here we use vi:

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

Navigate to the ExecStart line and change the -a port to 80:

...
ExecStart=/usr/sbin/varnishd \ -a :80 \
...

When you are done, save and close the file.

Reload the system daemon to apply the changes:

sudo systemctl daemon-reload

3. Configure Nginx to Work with Varnish Cache

The next step is to change the Nginx default listening port from 80 to 8080 so as not to listen on the same port as Varnish. Open the Nginx configuration file with your favorite text editor:

sudo vi /etc/nginx/nginx.conf

And change the listening port to 8080:

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

When you are done, save and close the file.

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Now open port 8080 through the firewall by executing the command below:

# sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
# sudo firewall-cmd --reload

Ensure that Nginx is configured as a backend server for the Varnish proxy:

sudo vi /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}

4. Testing Varnish Cache

Finally, you can test if the Varnish cache is enabled and working with the Nginx service with the curl command below:

curl -I http://localhost
Testing Varnish Cache AlmaLinux 9

This gives you the HTTP header information. If you rerun the command, it will show that Varnish cached response (NOTE that Age header):

curl -I http://localhost
Testing Varnish Cache with Nginx

It should work the same for valid domain names with DNS A record set.

Conclusion

At this point, you learn to Install Varnish Cache with Nginx on AlmaLinux 9. Nginx Varnish Cache on AlmaLinux 9 is used to improve website performance by caching content and reducing server load. Varnish sits in front of Nginx, handling requests faster by serving cached pages instead of querying the backend server.

Hope you enjoy it. You may also like these articles:

Secure Apache with Let’s Encrypt on AlmaLinux 9

Install and Configure NTP Server and Client on AlmaLinux 9

Varnish Cache Setup with Nginx on Debian 12

Install Nginx Varnish Cache on Rocky Linux 9

Varnish Cache with Apache Setup on AlmaLinux 9

FAQs

Why use Varnish with Nginx?

Varnish caches static and dynamic content, reducing server load and improving response time, while Nginx handles additional optimizations and SSL termination.

How do I monitor Varnish cache activity?

You can use the command: varnishstat

Can Varnish cache HTTPS traffic?

No, Varnish does not handle SSL directly. You need to configure Nginx to terminate SSL and pass requests to Varnish.

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!