Varnish is a popular caching reverse proxy that can be used to improve the performance and scalability of web applications. In this article, we will go over the steps for installing Varnish 7 on Rocky Linux 8, along with Nginx as the web server.
Before we begin, make sure you have a fresh installation of Rocky Linux 8 with a user that has sudo
privileges.
Install Nginx
First, we need to install Nginx as the web server. To do this, run the following command:
sudo dnf install nginx
After the installation is complete, start the Nginx service and enable it to start automatically on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
To verify that Nginx is working correctly, access the URL http://your-server-ip
in a web browser. You should see the default Nginx welcome page.
Install Varnish
Next, we need to install Varnish. To do this, run the following command:
Copy code
sudo dnf install varnish
After the installation is complete, open the Varnish configuration file /etc/varnish/default.vcl
and update the backend
settings to point to the Nginx web server. For example:
Copy code
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Next, open the Nginx configuration file /etc/nginx/nginx.conf
and update the server
block to listen on port 8080
instead of 80
. For example:
Copy code
server {
listen 8080;
...
}
Save the file and exit.
Configure Varnish and Nginx
Now we need to configure Varnish and Nginx to work together. To do this, open the Nginx configuration file /etc/nginx/nginx.conf
and add the following lines at the top of the file:
include /etc/varnish/nginx.vcl;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
Next, create a new file /etc/varnish/nginx.vcl
with the following content:
vcl 4.0;
import std;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405, "Method not allowed"));
}
return (purge);
}
}
sub vcl_backend_response {
set beresp.grace = 3h;
}
Save the file and exit.
Finally, open the Varnish configuration file /etc/varnish/varnish.params
and update the VARNISH_LISTEN_PORT
and VARNISH_ADMIN_LISTEN_ADDRESS
variables as follows:
VARNISH_LISTEN_PORT=6081
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
Save the file and exit.
Now that we have installed and configured Varnish and Nginx, we can start the Varnish and Nginx services and test that everything is working as expected.
To start the Varnish and Nginx services, run the following commands:
sudo systemctl start varnish
sudo systemctl start nginx
To verify that Varnish is working correctly, access the URL http://your-server-ip
in a web browser. You should see the default Nginx welcome page, but this time it will be served by Varnish. You can verify this by checking the X-Varnish
and Age
headers in the HTTP response.
Configure the firewall
If you have a firewall enabled on your system, you will need to allow access to the Varnish and Nginx ports in order to access them from a remote machine.
To do this, run the following commands to open ports 80 and 6081 and reload the firewall rules:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-port=6081/tcp
sudo firewall-cmd --reload
Conclusion
In this article, we went over the steps for installing and configuring Varnish 7 and Nginx on Rocky Linux 8. We installed the required packages, updated the configuration files, and started the Varnish and Nginx services. We also covered how to configure the firewall to allow access to Varnish and Nginx.
I hope this tutorial has been helpful for you. Happy caching!