Install Varnish Cache for Nginx on Centos 7

In this tutorial, we intend to teach you How To Install Varnish Cache for Nginx on Centos 7.

Varnish Cache is a robust web accelerator that allows, content-rich dynamic websites to endure high traffic. It enables web pages to load faster, by as much as 1000%. Thus, the average waiting time for a page to load is drastically reduced.

Install Varnish Cache for Nginx on Centos 7

To install Varnish Cache on your server, you need some requirements.

Requirements

First, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Centos 7.

Because you will install Varnish for Nginx, you must have Nginx installed on your server. You can follow this guide on How To Install Nginx on Centos 7.

Now follow the steps below to complete this guide.

Installing Varnish Cache on Centos 7

First, you must install some dependencies on your server with the following command:

sudo yum -y install pygpgme yum-utils

Then, you must add the varnish cache repo on Centos 7 by using the following command:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish60lts/script.rpm.sh | sudo bash

When you are finished, you will get the following output:

Output
The repository is setup! You can now install packages.

Next, update your local package index:

sudo yum -y update

Now you can use the following command to install Varnish on your server:

sudo yum install varnish

To get more details about your Varnish installation on Centos 7, you can use the following command:

rpm -qi varnish
Output
Name : varnish
Version : 6.0.10
Release : 1.el7
Architecture: x86_64
Install Date: Sat 23 Jul 2022 04:31:29 AM EDT
Group : System Environment/Daemons
Size : 7426012
License : BSD
Signature : (none)
Source RPM : varnish-6.0.10-1.el7.src.rpm
Build Date : Wed 12 Jan 2022 07:44:11 AM EST
Build Host : 65f33eb8a39c
Relocations : (not relocatable)
URL : https://www.varnish-cache.org/
Summary : High-performance HTTP accelerator
...

These are some key notes about Varnish:

  • 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 and /etc/systemd/system/multi-user.target.wants/varnish.service

At this point, you can start and enable your Varnish Cache with the following command:

sudo systemctl enable --now varnish

Verify varnish is active and running on Centos 7:

sudo systemctl status varnish
Output
● 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 Sat 2022-07-23 04:34:56 EDT; 28s ago
Process: 30322 ExecStart=/usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
Main PID: 30323 (varnishd)
CGroup: /system.slice/varnish.service
├─30323 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p fea...
└─30333 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p fea...

Configure Nginx to Work With Varnish Cache on Centos 7

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

Open the Nginx configuration file with your favorite text editor, here we use vi:

sudo vi /etc/nginx/nginx.conf

Find the following block and change the Listen port to 8080:

...
server {
listen 8080;
listen [::]:8080;
...

When you are done, save and close the file.

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

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

Now restart Nginx to apply the changes:

sudo systemctl restart nginx

Verify Nginx is running on port 8080 with the following command:

netstat -plant | grep -w 8080
Output
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 30579/nginx: master
tcp6 0 0 :::8080 :::* LISTEN 30579/nginx: master

At this point, you need to configure Varnish Cache.

Configure Varnish Cache

You must edit the Varnish Cache configuration file and set Listen port to 80.

Open the file with your favorite text editor, here we use vi:

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

Edit the line starting with ExecStart, and change it from 6081 to 80:

...
ExecStart=/usr/sbin/varnishd \
-a :80 \
...

When you are done, save and close the file.

Then restart the Varnish systemd service:

# sudo systemctl daemon-reload
# sudo systemctl restart varnish

Confirm Varnish used port with the following command:

sudo systemctl status varnish

Output
● 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 Sat 2022-07-23 04:48:16 EDT; 7s ago
Process: 30878 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
Main PID: 30879 (varnishd)
CGroup: /system.slice/varnish.service
├─30879 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p featu...
└─30889 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p featu...

Ensure that Nginx is configured as a backend server for the Varnish proxy:

sudo vi /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}

Testing Varnish Cache

Finally, you can test if the Varnish cache is enabled and working with the Nginx service with the curl command below:

curl -I http://localhost
Output
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 23 Jul 2022 08:51:32 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
ETag: "53762af0-12e1"
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
Output
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 23 Jul 2022 08:51:32 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
ETag: "53762af0-12e1"
X-Varnish: 32770 3
Age: 27
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

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

For more information, you can visit the Varnish Cache Documentation page.

Conclusion

At this point, you learn to Install and Configure Varnish Cache for Nginx on Centos 7.

Hope you enjoy it.

You may be interested in these articles:

Install and Configure Lighttpd Web Server on Centos 7

How To Install and Configure Nextcloud on Centos 7

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!