Install Varnish Cache on AlmaLinux 9
Step 1 – Install
dnf install epel-release -y
dnf install -y varnish
systemctl enable --now varnish
Step 2 – Basic /etc/varnish/default.vcl
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 5s;
.first_byte_timeout = 90s;
}
sub vcl_recv {
if (req.url ~ "\.(css|js|png|jpg|gif|ico|woff2?)$") {
unset req.http.cookie;
}
}
sub vcl_backend_response {
if (bereq.url ~ "\.(css|js|png|jpg|gif|ico|woff2?)$") {
set beresp.ttl = 1h;
}
}
Step 3 – Point Varnish to port 80, web server to 8080
Edit the Varnish systemd service or params file to set VARNISH_LISTEN_PORT=80,
then reconfigure Nginx/Apache to listen on port 8080.
Step 4 – Reload
systemctl restart varnish 2>/dev/null || rc-service varnish restart
varnishstat