Saturday, November 16, 2024
Google search engine
HomeGuest BlogsInstall Varnish Cache 6 for Apache/Nginx on CentOS 8|RHEL 8

Install Varnish Cache 6 for Apache/Nginx on CentOS 8|RHEL 8

Varnish Cache, also called caching HTTP reverse proxy is a leading web application accelerator. It often sit in front of a web server such as Nginx or Apache and its main work is to cache the contents of these servers for better load speed. Varnish Cache has been tested to speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

Varnish Cache stores web pages in memory so web servers don’t have to create the same web page over and over again. This enables it to serve web pages much faster than any application server would; giving the website a significant speed up.

In this tutorial, we’ll cover how you can install and configure Varnish Cache 6 LTS on CentOS 8 / RHEL 8 system using Apache httpd/Nginx web server. The Varnish cache can be installed on a Virtual Machine running in the cloud or your datacenter. The choice is yours – depending on where the applications using it are running.

Step 1: Update System

Ensure your system is updated.

sudo dnf -y update

Step 2: Install Varnish Cache package

Install Varnish Cache 6 on CentOS 8 / RHEL 8 Linux machine by running the commands.

sudo dnf install @varnish

Press the y key when prompted to continue with the installation.

Transaction Summary
===================================================================================================================================================
Install  22 Packages

Total download size: 41 M
Installed size: 107 M
Is this ok [y/N]: y

More information on installed package can be checked using:

$ rpm -qi varnish
Name        : varnish
Version     : 6.0.8
Release     : 2.module+el8.7.0+1094+66cdd070.1
Architecture: x86_64
Install Date: Mon 16 Jan 2023 09:51:13 PM UTC
Group       : System Environment/Daemons
Size        : 3110640
License     : BSD
Signature   : RSA/SHA256, Mon 28 Nov 2022 05:16:04 PM UTC, Key ID 15af5dac6d745a60
Source RPM  : varnish-6.0.8-2.module+el8.7.0+1094+66cdd070.1.src.rpm
Build Date  : Mon 28 Nov 2022 05:11:53 PM UTC
Build Host  : ord1-prod-x86build004.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : [email protected]
Vendor      : Rocky
URL         : https://www.varnish-cache.org/
Summary     : High-performance HTTP accelerator
.....

Key notes about Varnish installation on CentOS 8 / RHEL 8.

  • The main Varnish configuration file is /etc/varnish/default.vcl
  • Varnish secret file: /etc/varnish/secret
  • Varnish Cache executable binary: /usr/sbin/varnishd
  • Varnish Systemd unit file: /lib/systemd/system/varnish.service

Step 3: Start and enable Varnish service

Now that we’ve installed Varnish Cache on CentOS 8 / RHEL 8, let’s start the service and set it to start at boot.

sudo systemctl enable --now varnish

Confirm the service status:

$ systemctl status varnish
varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-01-16 21:51:38 UTC; 4s ago
  Process: 4596 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 4597 (varnishd)
    Tasks: 217
   Memory: 94.5M
   CGroup: /system.slice/varnish.service
           ├─4597 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
           └─4607 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Warnings:
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: VCL compiled.
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Debug: Version: varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Version: varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Platform: Linux,4.18.0-425.3.1.el8.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Debug: Platform: Linux,4.18.0-425.3.1.el8.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Debug: Child (4607) Started
Jan 16 21:51:37 rocky8.mylab.io varnishd[4597]: Child (4607) Started
Jan 16 21:51:38 rocky8.mylab.io varnishd[4597]: Child (4607) said Child starts
Jan 16 21:51:38 rocky8.mylab.io systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

Step 4: Configure Varnish Cache for Nginx/Apache

We stated earlier that Varnish Cache is a caching HTTP reverse proxy
which sits in front of a web server. The goal is to speed up web
servers.

Install your favorite web server – This demo shows the installation of Nginx/Apache HTTPD server.

# Apache
sudo dnf -y install @httpd

# Nginx
sudo dnf -y install @nginx

1. Configure Nginx to Work With Varnish Cache

By default, Nginx listens on TCP port 80, you need to change Listen port to 8080. Varnish Cache will use port 80.

$ sudo vi /etc/nginx/nginx.conf
.....
server {
        listen       8080 default_server;
        listen       [::]:8080 default_server;
        ....
}

If using Virtual Hosting feature, edit the relevant configuration file, e.g.

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

Then restart Nginx.

sudo systemctl restart nginx

Confirm your settings.

$ ss -tunelp | grep 8080
tcp   LISTEN  0       128                  0.0.0.0:8080           0.0.0.0:*      ino:106078 sk:11 <->                                                           
tcp   LISTEN  0       128                     [::]:8080              [::]:*      ino:106079 sk:15 v6only:1 <-> 

2. Configure Apache to Work With Varnish Cache

If you’re using Apache web server, set Listen port to 8080.

$ sudo vi /etc/httpd/conf/httpd.conf
...
Listen 8080

A single sed command can also be used.

sudo sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf

Restart httpd service after the change.

sudo systemctl restart httpd

3. Configure Varnish Cache Server

Now edit Varnish Cache configuration file and set Listen port to 80.

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

Edit the line starting with ExecStart, and change from:

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

To:

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

Then restart varnish systemd service.

sudo systemctl daemon-reload
sudo systemctl restart varnish

Confirm Varnish used port.

$ systemctl status  varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-11-04 22:20:53 EAT; 7s ago
  Process: 6333 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 6335 (varnishd)
    Tasks: 217
   Memory: 93.7M
   CGroup: /system.slice/varnish.service
           ├─6335 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
           └─6345 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Nov 04 22:20:52 centos8.novalocal systemd[1]: Stopped Varnish Cache, a high-performance HTTP accelerator.
Nov 04 22:20:52 centos8.novalocal systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Child (6345) Started
Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Child (6345) Started
Nov 04 22:20:53 centos8.novalocal varnishd[6335]: Child (6345) said Child starts
Nov 04 22:20:53 centos8.novalocal systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

Ensure that Nginx/Apache is configured as a backend server for Varnish proxy.

$ sudo vi /etc/varnish/default.vcl
.....
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
.......

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

For Multiple backends, your configuration will look this:

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

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

But you’ll need to tell Varnish where to send the difference URL
using vcl_recv. Lets say 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;
    }
}

See more on using Multiple backends.

Step 5: Test Varnish Cache on Nginx/Apache

Finally, we test if Varnish cache is enabled and working with Apache/Nginx service with the curl command below:

$ curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Mon, 04 Nov 2022 19:22:31 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2022 21:16:24 GMT
ETag: "5d9bab28-fd9"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

This gives you the HTTP header information.

If you rerun the command, it will show that Varnish cached response (NOTE that Age header):

$ curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Jan 16 21:51:38 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2022 21:16:24 GMT
ETag: "5d9bab28-fd9"
X-Varnish: 5 3
Age: 66
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

It should work same for valid domain names with DNS A record set.

$ curl -I https://blog.geeksforgeeks.org 
HTTP/2 200 
date: Tue, 28 May 2022 09:09:20 GMT
content-type: text/html
set-cookie: __cfduid=de1acba723fc04cdfb58c84af80ac1b401559034559; expires=Wed, 27-May-20 09:09:19 GMT; path=/; domain=.neveropen.tech; HttpOnly; Secure
last-modified: Fri, 10 May 2022 08:08:40 GMT
x-varnish: 65557 17
age: 22
via: 1.1 varnish (Varnish/6.0)
accept-ranges: bytes
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 4ddf16ce9d86cb77-MBA

We have installed and configured Varnish Cache on CentOS 8 / RHEL 8 for Nginx and Apache web servers. Visit the Varnish Getting started and the The Varnish Users Guide pages to learn more about configuring and optimizing Varnish.

Check other related guides:

RELATED ARTICLES

Most Popular

Recent Comments