Share your love
Install Varnish Cache for Nginx on AlmaLinux 8
In this article, we want to teach you How To Install Varnish Cache for Nginx on AlmaLinux 8.
Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configures it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.
Install Varnish Cache for Nginx on AlmaLinux 8
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 8.
Also, you need Nginx installed on your server. You can visit the installation part of this guide on How To Install Nginx on AlmaLinux 8.
Now follow the steps below to complete this guide.
Installing Varnish Cache on AlmaLinux 8
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/varnish71/script.rpm.sh | sudo bash
When your download is completed, install the package with the following command:
sudo dnf install varnish
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 8:
sudo systemctl status varnish
Output ● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor pre> Active: active (running) since Mon 2022-07-25 07:01:13 EDT; 44s ago Main PID: 92041 (varnishd) Tasks: 217 Memory: 93.1M CGroup: /system.slice/varnish.service ├─92041 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s m> └─92051 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s m>
Configure Varnish on AlmaLinux 8
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 w 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 with 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: Mon, 25 Jul 2022 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: Mon, 25 Jul 2022 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 for Nginx on AlmaLinux 8.
Hope you enjoy it.
Thank you for your sharing. Can I use for Rocky Linux
Hey there, Yes you can use it for rocky Linux or other RHEL 8 version