How to Install Varnish Cache for NGINX in CentOS


varnish cache

Varnish Cache allows you to speed up web server performance. This not only reduces your server load but also data bandwidth. Here’s how to install Varnish Cache for NGINX in CentOS.

 

How to Install Varnish Cache for NGINX in CentOS

Here are the steps to Install Varnish Cache for NGINX in CentOS. The same steps can be used for installing Varnish Cache in RHEL and Fedora systems.

 

1. Install NGINX Web Server in CentOS

If you have already installed NGINX, you can skip to step 4. Otherwise, open a terminal session or SSH into your CentOS system and run the following command to install Varnish Cache


$ yum install nginx

If you are asked for a confirmation, type ‘Y’ and press Enter

 

 

2. Enable Autostart

After installation is complete, start your NGINX server and enable it to automatically start at system boot.


$ systemctl start nginx
$ systemctl enable nginx
$ systemctl status nginx

 

3. Update Firewall rules

Update firewall rules to allow traffic via port 80. Reload NGINX after you update firewall rules.


$ firewall-cmd --zone=public --permanent --add-port=80/tcp
$ firewall-cmd --reload
$ firewall-cmd --zone=public --permanent --add-port=8080/tcp
$ firewall-cmd --reload

 

4. Enable EPEL repository

We will install the official Varnish Cache package. Before that you need to enable EPEL repository and install the dependency packages as shown


$ yum install -y epel-release

Next, install pygpgme and yum-utils that are required for Varnish to function properly.


$ yum install pygpgme yum-utils

 

Create a file vi /etc/yum.repos.d/varnishcache_varnish5.repo with the repository configuration as shown below


$ vi /etc/yum.repos.d/varnishcache_varnish5.repo

 

Ensure that you replace el and 7 below with your Linux distribution and version


[varnishcache_varnish5]
name=varnishcache_varnish5
baseurl=https://packagecloud.io/varnishcache/varnish5/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish5/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

[varnishcache_varnish5-source]
name=varnishcache_varnish5-source
baseurl=https://packagecloud.io/varnishcache/varnish5/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish5/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

 

Next, update your yum cache and install varnish cache 5 package. When you are asked for confirmation, type ‘Y’ and press enter


$ yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5'
$ yum install varnish

 

5. Verify Installation

After installation, you will find the main varnish executable in /usr/bin/varnishd and config file at /etc/varnish

  • /etc/varnish/varnish.params – varnish environment configuration file
  • /etc/varnish/default.vcl – main varnish configuration file
  • /etc/varnish/secret – varnish secret file

Confirm location of varnish executable and its version(Optional)


$ which varnishd
$ varnishd -V

 

6. Configure NGINX

By default, NGINX runs on port 80. We need to change NGINX port to 8080 so that it runs behind Varnish Cache, which will run on port 80.

Open NGINX config file at /etc/nginx/nginx.conf and look for the line “listen 80”. Change it to 8080 as shown below.


$ vi /etc/nginx/nginx.conf

change nginx port

 

 

7. Make Varnish Listen to port 80

Next, we need to make Varnish listen to port 80. You can do that by opening Varnish environment config file, look for the parameter VARNISH_LISTEN_PORT which stores the port number Varnish listens to. By default, it is 6081. Change it to 80, as shown.


$ vi /etc/varnish/varnish.params

 

Configure Varnish Cache

 

 

8. Setup NGINX as backend server

Next, we configure NGINX to work as a backend server for Varnish proxy. Open varnish config file at /etc/varnish/default.vcl with a text editor.


$ vi /etc/varnish/default.vcl

 

Look for the “backend” section, point its IP and port number to those of your NGINX server. If both NGINX and Varnish are at the same location, use “127.0.0.1” as IP.


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

 

9. Restart Varnish and NGINX

After you have configured both NGINX and Varnish, restart both of them to apply the changes


$ systemctl restart nginx
$ systemctl start varnish
$ systemctl enable varnish
$ systemctl status varnish

 

10. Test Varnish Cache


$ curl -I http://localhost

This will display the HTTP response, where you can see that it has been served via Varnish

Test Varnish Cache on Nginx

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!