How to Install Varnish Cache 5.2 for Nginx on CentOS 7

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.


  1. A CentOS 7 with Apache installed
  2. A CentOS 7 with static IP address

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
Install Nginx on CentOS 7
Install Nginx on CentOS 7

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
Start and Enable Nginx at Boot
Start and Enable Nginx at Boot

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
Open Nginx Port on Firewall
Open Nginx Port on Firewall

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:



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 
Install Varnish Cache
Install Varnish Cache

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
Verify Varnish Cache Installation
Verify Varnish Cache Installation

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

Configure Nginx Listening Port
Configure Nginx Listening Port

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
Configure Varnish Cache
Configure Varnish Cache

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 = "";
    .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
Test Varnish Cache on Nginx
Test Varnish Cache on Nginx

You can find additional information from the Varnish Cache Github Repository:

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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

9 thoughts on “How to Install Varnish Cache 5.2 for Nginx on CentOS 7”

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

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


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.