6 C
Amsterdam
Friday, December 4, 2020

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

Must read

How To Configure Slave BIND DNS Server On Ubuntu 20.04

In our previous guide we covered the setup of Master(Primary) DNS Server. Here we'll learn that how to configure slave bind dns server on...

Install Fossil on Linux – An alternative to Git

Hello, friends. In this post, we will talk about a serious alternative to Git. In a few words, we'll show you how to install...

How to backup and restore BCD in Windows 10

Hi! Boot Configuration Data (BCD) is a set of data where the system boot information is stored. In addition, from there the operating systems...

How To Install and Configure Master BIND DNS Server On Ubuntu 20.04

Today we are going to learn that how to install and configure Master BIND DNS server on Ubuntu 20.04. As you all know that...

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 Configure Slave BIND DNS Server On Ubuntu 20.04

In our previous guide we covered the setup of Master(Primary) DNS Server. Here we'll learn that how to configure slave bind dns server on...

Install Fossil on Linux – An alternative to Git

Hello, friends. In this post, we will talk about a serious alternative to Git. In a few words, we'll show you how to install...

How to backup and restore BCD in Windows 10

Hi! Boot Configuration Data (BCD) is a set of data where the system boot information is stored. In addition, from there the operating systems...

How To Install and Configure Master BIND DNS Server On Ubuntu 20.04

Today we are going to learn that how to install and configure Master BIND DNS server on Ubuntu 20.04. As you all know that...

Diskpart is a very useful tool for managing storage in Windows 10.

Hello! Diskpart is an internal tool included in Windows systems that allows you to manage storage units. Indeed, it is possible to manage internal...
x