Share your love
Install Varnish Cache with Nginx on AlmaLinux 9
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.
Steps To Install Varnish Cache with Nginx on AlmaLinux 9
To install Varnish for Nginx, 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 this guide.
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 and 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
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
Output Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor p> Active: active (running) since Sat 2022-10-08 07:25:48 EDT; 28s ago Main PID: 18168 (varnishd) Tasks: 217 Memory: 98.5M CPU: 1.096s CGroup: /system.slice/varnish.service ├─18168 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /et> └─18190 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /et>
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
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"; }
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
Output
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sat 2022-10-08 11:13:53 GMT
Content-Type: text/html
Content-Length: 3854
Last-Modified: Mon, 19 Apr 2021 10:05:11 GMT
ETag: "607d55d7-f0e"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive
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
Output
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sat 2022-10-08 11:13:53 GMT
Content-Type: text/html
Content-Length: 3854
Last-Modified: Mon, 19 Apr 2021 10:05:11 GMT
ETag: "607d55d7-f0e"
X-Varnish: 32770 3
Age: 22
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive
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.
Hope you enjoy it.
You may be like these articles: