16

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

 2 years ago
source link: https://computingforgeeks.com/install-varnish-cache-for-apache-nginx-on-centos-rhel-8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Install Varnish Cache 6 for Apache/Nginx on CentOS 8
Search

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 6 on CentOS 8 / RHEL 8

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.

Dependencies resolved.
===================================================================================================================================================
 Package                              Arch                   Version                                               Repository                 Size
===================================================================================================================================================
Installing group/module packages:
 varnish                              x86_64                 6.0.2-1.module_el8.0.0+19+b2cdb875                    AppStream                 2.6 M
 varnish-modules                      x86_64                 0.15.0-4.module_el8.0.0+19+b2cdb875                   AppStream                  83 k
Installing dependencies:
 annobin                              x86_64                 8.64-1.el8                                            AppStream                 187 k
 cpp                                  x86_64                 8.2.1-3.5.el8                                         AppStream                  10 M
 dwz                                  x86_64                 0.12-9.el8                                            AppStream                 109 k
 efi-srpm-macros                      noarch                 3-2.el8                                               AppStream                  22 k
 gcc                                  x86_64                 8.2.1-3.5.el8                                         AppStream                  23 M
 ghc-srpm-macros                      noarch                 1.4.2-7.el8                                           AppStream                 9.3 k
 go-srpm-macros                       noarch                 2-16.el8                                              AppStream                  14 k
 isl                                  x86_64                 0.16.1-6.el8                                          AppStream                 841 k
 ocaml-srpm-macros                    noarch                 5-4.el8                                               AppStream                 9.4 k
 openblas-srpm-macros                 noarch                 2-2.el8                                               AppStream                 7.9 k
 perl-srpm-macros                     noarch                 1-25.el8                                              AppStream                  11 k
 python-srpm-macros                   noarch                 3-37.el8                                              AppStream                  14 k
 python3-rpm-macros                   noarch                 3-37.el8                                              AppStream                  13 k
 qt5-srpm-macros                      noarch                 5.11.1-2.el8                                          AppStream                  11 k
 redhat-rpm-config                    noarch                 116-1.el8.0.1                                         AppStream                  82 k
 rust-srpm-macros                     noarch                 5-2.el8                                               AppStream                 9.2 k
 glibc-devel                          x86_64                 2.28-42.el8.1                                         BaseOS                    1.0 M
 glibc-headers                        x86_64                 2.28-42.el8.1                                         BaseOS                    465 k
 kernel-headers                       x86_64                 4.18.0-80.11.2.el8_0                                  BaseOS                    1.6 M
 libxcrypt-devel                      x86_64                 4.1.1-4.el8                                           BaseOS                     25 k
Installing module profiles:
 varnish/common                                                                                                                                   
Enabling module streams:
 varnish                                                     6                                                                                    

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.2
Release     : 1.module_el8.0.0+19+b2cdb875
Architecture: x86_64
Install Date: Mon 04 Nov 2019 10:08:22 PM EAT
Group       : System Environment/Daemons
Size        : 9467384
License     : BSD
Signature   : RSA/SHA256, Wed 31 Jul 2019 12:16:17 AM EAT, Key ID 05b555b38483c65d
Source RPM  : varnish-6.0.2-1.module_el8.0.0+19+b2cdb875.src.rpm
Build Date  : Sat 01 Jun 2019 10:15:53 PM EAT
Build Host  : x86-02.mbox.centos.org
Relocations : (not relocatable)
Packager    : CentOS Buildsys <[email protected]>
Vendor      : CentOS
URL         : https://www.varnish-cache.org/
Summary     : High-performance HTTP accelerator
Description :

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 Cache on CentOS 8 / RHEL 8

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 2019-11-04 22:12:12 EAT; 23s ago
  Process: 4975 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 4976 (varnishd)
    Tasks: 217
   Memory: 98.3M
   CGroup: /system.slice/varnish.service
           ├─4976 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
           └─4986 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

Nov 04 22:12:11 centos8.novalocal systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Child (4986) Started
Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Child (4986) Started
Nov 04 22:12:12 centos8.novalocal varnishd[4976]: Child (4986) said Child starts
Nov 04 22:12:12 centos8.novalocal systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

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

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
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.

$ sudo 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 2019-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 2019 19:22:31 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 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: Mon, 04 Nov 2019 19:22:31 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 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.computingforgeeks.com 
HTTP/2 200 
date: Tue, 28 May 2019 09:09:20 GMT
content-type: text/html
set-cookie: __cfduid=de1acba723fc04cdfb58c84af80ac1b401559034559; expires=Wed, 27-May-20 09:09:19 GMT; path=/; domain=.computingforgeeks.com; HttpOnly; Secure
last-modified: Fri, 10 May 2019 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:

Install Varnish Cache on CentOS 7

How to Install and Configure Memcached on CentOS 8 / RHEL 8

How To Create CentOS 8 Local Repository Mirrors With Rsync & Nginx


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK