How to Install Varnish cache 6 for Apache/Nginx on Centos / RHEL 8

Windows Articles

How to get Google Opinion Rewards in any Country

Greetings! Google Opinion Rewards is a cool app, which will allow you to earn credit, to spend it on the Google Play...

How to change the hostname on Ubuntu?

This simple tutorial will teach you how to change the hostname on Ubuntu. The steps we will tell you here will work...

How to install Screaming Frog SEO Spider on Ubuntu 20.04 / 18.04?

If you are working with blogs and content creation then you should know about SEO. Or in the best case, you have...

How to Configure a Firewall in Ubuntu 20.04 / Debian 10?

There is nothing safe from hackers on the Internet, however, we can implement security measures to be more protected against an attack. With this...

Gnome 3.38 available

Gnome OS is a Linux-based operating system, which has a huge domain in the Linux forums. On September 16, 2020, developers announced...

What is Varnish Cache?

As you know about Cache, it enhances the loading speed by saving web pages, cookies and other data. It saves into memory the requested data and provide faster results. Because of its faster response, it is also used in Linux like Centos to provide faster results. It is also called caching HTTP reverse proxy which is a leading web application accelerator, capturing the visits and boost upon delivery on again request. It saves the web servers time by providing them the same data as saved earlier. Hence, in this tutorial we are going to learn how you can install and configure Varnish Cache 6 on CentOS 8 / RHEL 8.

Step 1: Update System

Make sure your system is up-to-date.

sudo dnf -y update

Step 2: Install Varnish Cache 6 on CentOS 8 / RHEL 8

You can use the following command to install varnish cache on CentOS / RHEL 8.

sudo dnf install @varnish

Allow to continue with the installation process by pressing y key.

If you want to see more information on installed package you can type the following command.

rpm -qi varnish

Important things to Remember:

  • Main configuration file of Varnish can be found in /etc/varnish/default.vcl
  • Secret file of Varnish is in /etc/varnish/secret
  • Cashe executable binary is found in /usr/sbin/varnishd
  • Systemd unit file is in /lib/systemd/system/varnish.service

Step 3: Start and Enable Varnish Cache Services

Use the following command to start & enable Varnish Cache services.

sudo systemctl enable --now varnish

Make sure status is working properly

systemctl status varnish

Step 4: Configure Varnish Cache for Nginx/Apache Web Server

As Varnish sits in front of a web server as a caching HTTP Reverse Proxy, so it speeds up web servers.
In this tutorial we are using Nginx/Apache HTTPD server but you can use your favourite one.

For Apache

sudo dnf -y install @httpd

For Nginx

sudo dnf -y install @nginx
1. Configure Nginx with Varnish

By default, TCP port 80 is being used by Nginx, change it to listen to 8080 port because Varnish Cache will use port 80.

sudo vi /etc/nginx/nginx.conf

For Virtual Hosting feature, edit the relevant confiuguration file,

sudo vi /etc/nginx/conf.d/mysite.conf

After it, restart Nginx

sudo systemctl restart nginx

Double-check your settings

ss -tunelp | grep 8080
2. Configure Apache with Varnish

For Apache web server, make sure to set listen port to 8080.

sudo vi /etc/httpd/conf/httpd.conf

After it, restart Apache.

sudo systemctl restart httpd
3. Configure Varnish Cache Server

Set Listen port for Varnish Cache to 80.

sudo vi /etc/systemd/system/multi-user.target.wants/varnish.service

Edit the line starting with ExecStart, and change from:

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

To:

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

Now, restart Varnish systemd services.

sudo systemctl daemon-reload
sudo systemctl restart varnish

Make sure that Varnish is listening the correct port.

sudo systemctl status varnish

Also make sure that Nginx/Apache server is configured as a back-end server for Varnish proxy.

sudo vi /etc/varnish/default.vcl

Look port 8080 is configured on Apache/Nginx web server.

Your configuration will look like this for multiple back-ends,

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

But we’ll need to tell Varnish to where send the difference URL using vcl_recv. For example our java application should handle URL beginning with /java/

sub vcl_recv {
if (req.url ~ "^/java/") {
set req.backend_hint = java;
} else {
set req.backend_hint = default;
}
}

You can see more ones on using Multiple backends.

Step 5: Testing Varnish Cache on Nginx/Apache

After all, verify Varnish Cache working by testing it with Apache/Nginx service with the curl command:

curl -I http://localhost

It will provide you HTTP header information.

If you run this command again, it will show you that Varnish Cached Response (Note that Age header):

This will work same for other valid domain names with DNS record set.

curl -I https://osradar.com

We’ve installed & configured Varnish Cache on CentOS 8 / RHEL 8 for Nginx and Apache Web Server.

Visit the Varnish Getting Started and The Varnish Users Guide pages to learn more about configuring & optimizing Varnish.

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How to get Google Opinion Rewards in any Country

Greetings! Google Opinion Rewards is a cool app, which will allow you to earn credit, to spend it on the Google Play...

How to change the hostname on Ubuntu?

This simple tutorial will teach you how to change the hostname on Ubuntu. The steps we will tell you here will work...

How to install Screaming Frog SEO Spider on Ubuntu 20.04 / 18.04?

If you are working with blogs and content creation then you should know about SEO. Or in the best case, you have...

How to Configure a Firewall in Ubuntu 20.04 / Debian 10?

There is nothing safe from hackers on the Internet, however, we can implement security measures to be more protected against an attack. With this...

Gnome 3.38 available

Gnome OS is a Linux-based operating system, which has a huge domain in the Linux forums. On September 16, 2020, developers announced...
x