Varnish Cache (also referred to as Varnish) is an open source, high-performance HTTP accelerator designed for speeding up web servers. In our last article, we’ve explained how to setup Varnish Cache for Apache web server on CentOS 7.
In this article, we will show you how to install and use Varnish Cache 5.2 as a front-end to Nginx HTTP server in CentOS 7. This guide should also work on RHEL7.
Requirements
Step 1: Install Nginx Web Server on CentOS 7
1. Start by installing Nginx HTTP server from the default CentOS software repositories using the YUM package manager as follows.
# yum install nginx

2. When the installation completes, start Nginx service for now and enable it to start automatically at system boot.
# systemctl start nginx # systemctl enable nginx # systemctl status nginx

3. Now modify system firewall rules to permit inbound packets on port 80 using the commands below.
# firewall-cmd --zone=public --permanent --add-port=80/tcp # firewall-cmd --reload # firewall-cmd --zone=public --permanent --add-port=8080/tcp # firewall-cmd --reload

Step 2: Install Varnish Cache on CentOS 7
4. Now there are pre-compiled RPM packages for latest version of Varnish Cache 5 (i.e 5.2 at the time of writing), therefore you need to add official Varnish Cache repository.
Before that you need to enable the EPEL repository to install several dependency packages as shown.
# yum install -y epel-release
5. Next, install pygpgme, a package for handling GPG signatures and yum-utils, a collection of useful utilities that extend yum’s native features in various ways.
# yum install pygpgme yum-utils
6. Now create a file named /etc/yum.repos.d/varnishcache_varnish5.repo that contains the repository configuration below.
# vi /etc/yum.repos.d/varnishcache_varnish5.repo
Important: Make sure to replace el
and 7
in the config 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
7. Now run the command below to update your local yum cache and install varnish cache 5 package (do not forget to accept the GPG key by typing y
or yes
while installing the package):
# yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5' # yum install varnish

8. After installing Varnish Cache, the main executable will be installed as /usr/sbin/varnishd and varnish configuration files are located in /etc/varnish/:
- /etc/varnish/varnish.params – this is the varnish environment configuration file.
- /etc/varnish/default.vcl – this is the main varnish configuration file, it is written using vanish configuration language(VCL).
- /etc/varnish/secret – varnish secret file.
9. You can confirm that the Varnish installation was successful by seeing the location of the Varnish executable and version installed on your system.
$ which varnishd $ varnishd -V

Step 3: Configure Nginx to Work With Varnish Cache
10. In this step, you need to configure Nginx to work with Varnish Cache. By default Nginx listens on port 80, you should change the default Nginx port to 8080 so it runs behind Varnish caching.
Open the Nginx config file /etc/nginx/nginx.conf and find the line listen 80 and change it to listen 8080 as in the server block shown in the screen shot below.
# vi /etc/nginx/nginx.conf
11. Next, open the varnish environment configuration file and find the parameter VARNISH_LISTEN_PORT which specifies the port Varnish listens on, and change its value from 6081 to 80.
# vi /etc/varnish/varnish.params

12. Next, setup Nginx as a backend server for Varnish proxy, in the /etc/varnish/default.vcl configuration file.
# vi /etc/varnish/default.vcl
Find the backend section, and define the host IP and port. Below is the default backend configuration, set this to point to your actual content server.
backend default { .host = "127.0.0.1"; .port = "8080"; }
13. After performing all the necessary configurations, restart Nginx HTTPD and Varnish cache to effect the above changes.
# systemctl restart nginx # systemctl start varnish # systemctl enable varnish # systemctl status varnish
Step 4: Test Varnish Cache on Nginx
14. Finally, test if Varnish cache is enabled and working with the Nginx service using the cURL command below to view the HTTP header.
# curl -I http://localhost

You can find additional information from the Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache
In this tutorial, we explained how to setup Varnish Cache 5.2 for Nginx HTTP server on CentOS 7. Use the comment form below to send us any queries or extra ideas.
Need some input help, please!
I keep getting an http/1.1 503 Backend fetch failed using the
curl -I http://localhost
command.My /etc/varnish/default.vcl is active running. I’ve been over everything countless times and everything looks good any anyone have an idea of how to help?
Can you tell me where is the default.vcl located after installation?
@Gayatri
You can locate it in /etc/varnish/default.vcl. But when you install from source, this file is likely not to be available. You need to create it.
How do I delete it. I want to remove
Removing a package installed from source is a little of a challenge, because the system package manager normally doesn’t recognize it(as in keeping track of all its files and documentation etc..).
But you can try commands like:
$sudo make uninstall #if you still have the compilation directory
Or use find together with rm command(to locate and remove all package files), see the receptive man pages for more information.
@TEst
Sorry, respective* man pages.
Hello. Please help me.
[[email protected] varnish-5.1.2]# /usr/local/sbin/varnishd -a :80 -b https://chiasewp.com:8080
Error: Could not get socket :80: Address already in use
(-? gives usage)
@Việt
There is another process using port 80 on your machine, kill that process and run varnish one more time like this.
First find process ID using netstat and grep command below:
$sudo netstat -lntp | grep ‘:80’
Then kill it using its PID:
$sudo kill -9
Lastly, try to run varnish now.
For more info on how to find process listening on a certain port, read through: https://www.tecmint.com/find-out-which-process-listening-on-a-particular-port/
nginx run both port 80 and 8080 with the same PID