How to Install Varnish Cache with Nginx on CentOS 8

How to Install Varnish Cache with Nginx on CentOS 8

Varnish is an HTTP accelerator designed for content-heavy dynamic websites as well as APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and Nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.

Varnish Cache is a powerful, open-source HTTP engine/reverse HTTP proxy that can speed up a website by up to 1000 percent by doing exactly what its name implies: caching (or storing) a copy of a webpage the first time a user visits. And Varnish Cache stores content in pluggable modules called storage backends. It does this via its internal stevedore interface.

Varnish can be used for caching both static and dynamic content on websites. In other words, it is a web application accelerator. Simply, Varnish cache is an intermediator between the client and the webserver. It serves the stored content in its memory.

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability.

Install varnish cache with Nginx on CentOS 8; Through this tutorial, we will learn how to set up Varnish caching with Nginx on CentOS 8. And also will set up Nginx to listen on port 8080 and work as a backend server, then configure Varnish to listen on default HTTP port 80.

How to Install Varnish Cache with Nginx on CentOS 8

Follow the following steps to install and configure varnish cache with Nginx on CentOS 8:

  • Step 1 – Update System Packages
  • Step 2 – Install Varnish Cache
  • Step 3 – Install and Configure Nginx
  • Step 4 – Configure Varnish to Work with Nginx
  • Step 5 – Configure Nginx as a Backend Server for Varnish
  • Step 6 – Verify Varnish Cache

Step 1 – Update System Packages

First of all, open terminal or command line and execute the following command into it to update base system with the latest available packages:

dnf update -y

Step 2 – Install Varnish Cache

By default, the Varnish package is available in the CentOS 8 system. Also, we can install it by just executing the following command on command line or terminal:

dnf install varnish -y

Once the Varnish installation is completed, execute the following command on command line or termnal to start the Varnish service and enable it to start after system reboot:

systemctl start varnish
systemctl enable varnish

After that, verify the status of Varnish with the following command:

systemctl status varnish

By default, Varnish listens on port 6081. We can verify it using the following command:

netstat -ltnp | grep 6081

We should get the following output:

tcp        0      0 0.0.0.0:6081            0.0.0.0:*               LISTEN      1508/varnishd      
tcp6       0      0 :::6081                 :::*                    LISTEN      1508/varnishd      

Step 3 – Install and Configure Nginx

Execute the following command on the command line or terminal to install the Nginx web server:

dnf install nginx -y

After installing the Nginx web server, we need to configure Nginx with the Varnish cache.

So, we will need to change the Nginx web server listening port from 80 to 8080.

We can do it by editing the file /etc/nginx/nginx.conf. So, execute the following command on command line or terminal to edit it:

nano /etc/nginx/nginx.conf

Find the following line:

listen       80 default_server;
listen       [::]:80 default_server;

And replace it with the following line:

listen       8080 default_server;
listen       [::]:8080 default_server;

Save and close the file, then start the Nginx service and enable it to start after system reboot:

systemctl start nginx
systemctl enable nginx

Note: If we are using virtual hosting, then configure the relevant configuration file.

Step 4 – Configure Varnish to Work with Nginx

By default, Varnish is configured to listen on port 6081, so we will need to configure it to listen on port 80. You can configure it by editing varnish.service configuration file:

nano /usr/lib/systemd/system/varnish.service

Find the following line:

ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

And replace it with the following line:

ExecStart=/usr/sbin/varnishd -a :80 -f/etc/varnish/default.vcl -s malloc,256m

Save and close the file, then reload the systemd daemon with the following command:

systemctl daemon-reload

Next, restart the Varnish service to apply the changes:

systemctl restart varnish

Step 5 – Configure Nginx as a Backend Server for Varnish

Using the following commands, we will configure Nginx as a backend server for the Varnish proxy.

We can configure it by editing the file /etc/varnish/default.vcl:

nano /etc/varnish/default.vcl

Check to ensure the following lines are uncommented:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Save and close the file when you are finished.

Note: Change the line 127.0.0.1 with your Nginx server IP address if your Nginx server resides on the other host. Change the port 8080 with the port number of your Nginx server.

Step 6 – Verify Varnish Cache

Finally, the Varnish cache is configured to work with the Nginx webserver. And test whether caching is working or not.

We can test it using the curl command:

curl -I http://your-server-ip

Conclusion

Through this tutorial, we have successfully installed and configured Varnish Cache with an Nginx web server on CentOS 8.

Recommended CentOS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *