Share your love
Install Varnish Cache with Nginx on Rocky Linux 9
In this guide, we want to show you to Install Varnish Cache with Nginx on Rocky Linux 9.
Varnish is a reverse proxy that makes a copy of the page in the memory to serve it out when the page will be re-visited. The copy will be made based on request headers like URL and Request Method. This is known as Full Page Cache.
Varnish Cache ensures pages of webshops do not have to be loaded over again. The performance and page load will benefit a lot from Varnish Cache. And that is exactly what you want: a lightning-fast webshop for all your visitors.
Steps To Install Varnish Cache with Nginx on Rocky Linux 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 Rocky Linux 9.
Also, you need Nginx installed on your server. You can visit the installation part of this guide on How To Install Nginx on Rocky Linux 9.
Now follow the steps below to complete this guide.
Install Varnish Cache on Rocky Linux 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 running the command below:
# . /etc/os-release # sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-${VERSION_ID%%.*}.noarch.rpm
Download Varnish
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 by using the curl command:
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 Rocky Linux 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 Rocky Linux 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
Configure Firewall For Varnish Cache
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
At this point, ensure that Nginx is configured as a backend server for the Varnish proxy on Rocky Linux 9:
sudo vi /etc/varnish/default.vcl
It should be like this:
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 Rocky Linux 9.
Hope you enjoy it. Also, you may be like these articles: