Varnish Cache

How to Install Varnish with Nginx on AlmaLinux 9

Varnish is a popular open-source web application accelerator, also known as a caching HTTP reverse proxy, that is used to speed up dynamic web content delivery. It caches frequently accessed content and serves it directly from memory, reducing the load on the application servers and reducing the response time for users.

Varnish sits between the client and the web server and acts as an intermediary. When a user requests a web page, Varnish checks if the page is already in its cache. If it is, Varnish serves the page directly from its cache, without having to request it from the web server. If the page is not in the cache, Varnish requests it from the web server and caches it for future use.

Varnish also allows users to configure cache policies and rules to optimize caching behavior, and provides tools for monitoring and analyzing cache performance. It is commonly used by high-traffic websites to improve their performance and scalability.

In this tutorial, we will show you how to install  and configure Varnish with Nginx on AlmaLinux 9.

Step 1: Update Operating System

Update your AlmaLinux 9 operating system to make sure all existing packages are up to date:

# dnf update

Also, install the EPEL package manager to allow Varnish dependencies to be installed:

# dnf install epel-release 

Step 2: Install Nginx

You can install Nginx via dnf package manager by executing the following command.

# dnf install nginx

Nginx does not start automatically when it is installed. You can start the Nginx service and configure it to run on startup by entering the following commands:

# systemctl start nginx
# systemctl enable nginx

Verify the status of the Nginx service using systemctl status command:

# systemctl status nginx

Output:

 nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
    Drop-In: /usr/lib/systemd/system/nginx.service.d
             └─php-fpm.conf
     Active: active (running)
   Main PID: 1720 (nginx)
      Tasks: 2 (limit: 5740)
     Memory: 2.3M
        CPU: 28ms
     CGroup: /system.slice/nginx.service
             ├─1720 "nginx: master process /usr/sbin/nginx"
             └─1721 "nginx: worker process"

If firewalld is enabled consider allowing HTTP and HTTPS services:

$ sudo firewall-cmd --permanent --add-service={http,https}
$ sudo firewall-cmd --reload

Now you need to configure Nginx to work with Varnish Cache.

Open the Nginx config file /etc/nginx/nginx.conf :

 # nano /etc/nginx/nginx.conf

Search for the Listen line and change it to 8080:

.....   
    server {
        listen       8080;
        listen       [::]:8080;
        server_name  _;
        root         /usr/share/nginx/html;
 .....

Save and close the file. Then restart the Nginx service to apply the changes:

# systemctl restart nginx

Step 3: Install Varnish 7

First disable the Varnish service from the dnf package manager:

# dnf module disable varnish

By default, the latest version of Varnish is not available on the AlmaLinux 9 base repository.

So you need to run the following command to add the latest Varnish repository to your system:

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

After the repository is added, install Varnish using the following command:

# dnf install varnish

Check the installed version with the following command:

# varnishd -V

Output:

varnishd (varnish-7.3.0 revision 84d79120b6d17b11819a663a93160743f293e63f)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2023 Varnish Software

Now start and enable Varnish (to start automatically upon system boot):

# systemctl start varnish
# systemctl enable varnish

Verify the status using the following command:

# systemctl status varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled)
Active: active (running)
Main PID: 4266 (varnishd)
Tasks: 217
Memory: 113.4M
CPU: 2.074s
CGroup: /system.slice/varnish.service
├─4266 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /etc/varnish/default.vcl -P /run/varnish/varnishd.pid -p feature=+http2 -s malloc>
└─4280 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /etc/varnish/default.vcl -P /run/varnish/varnishd.pid -p feature=+http2 -s malloc>

Step 4: Configure Varnish Cache

The default Varnish listening port is 6081. Now we need to configure Varnish to listen on port 80.

You can do it by editing /usr/lib/systemd/system/varnish.service file:

# nano /usr/lib/systemd/system/varnish.service

Add the configuration to change the port 6081 to 80 as shown below:

...
ExecStart=/usr/sbin/varnishd \
-a :80 \
-a localhost:8443,PROXY \
-f /etc/varnish/default.vcl \
-P %t/%N/varnishd.pid \
-p feature=+http2 \
-s malloc,256m
ExecReload=/usr/sbin/varnishreload
...

After the changes, restart the Varnish service for the changes to take effect.

# systemctl daemon-reload
# sudo systemctl restart varnish

Step 5: Test Varnish Cache

Test if Varnish cache is enabled and working with the Nginx service using the curl command:

# curl -I http://your-IP-address

Output:

HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 06 Apr 2023 11:22:04 GMT
Content-Type: text/html
Content-Length: 4681
Last-Modified: Sat, 09 Oct 2021 17:49:21 GMT
ETag: "6161d621-1249"
X-Varnish: 2
Age: 0
Via: 1.1 192.168.1.150 (Varnish/7.3)
Accept-Ranges: bytes
Connection: keep-alive

This means that your Varnish Cashe is active and running on your server.

Comments and Conclusion

In the tutorial, you have learned how to install and configure Varnish on AlmaLinux 9.

For additional help or useful information, you can check the official Varnish website.

Leave a Reply

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