install varnish cache for nginx

How To Install Varnish Cache for NGINX in Centos

Varnish is a high-performance cache module that improves website speed and performance with caching. Here’s how to install varnish cache for NGINX server in CentOS, Redhat, Fedora Linux.


How To Install Varnish Cache for NGINX in Centos

Here are the steps to install varnish cache for NGINX server in CentOS, Redhat, Fedora linux.


1. Install NGINX on CentOS

If you have already installed NGINX in CentOS, skip to the next step. Else open terminal and run the following commands to install NGINX.

# yum install nginx

If you get a yes/no prompt, enter ‘y’ or ‘Y’ to complete installation.

Run the following command to start NGINX and enable it to autostart during system boot.

# systemctl start nginx
# systemctl enable nginx

Bonus Read : NGINX SSL Configuration Step by Step


2. Add EPEL Repository

Some of Varnish cache dependencies require EPEL repository setup. Run the following command to install it.

# yum install -y epel-release


3. Install dependencies

Install the dependencies required for varnish cache.

# yum install pygpgme yum-utils

Bonus Read : How to Prevent Image Hotlinking in NGINX


4. Add Varnish Cache Repository

Open the following file

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

and copy paste the following code in it. Make sure you replace /el/7 with your Linux version. In this case, we have used 7 for CentOS 7.



Save and close the file.

Bonus Read : How to harden NGINX Server


5. Install Varnish Cache

Run the following command to install varnish cache in CentOS. Enter y or Y or yes in case you see any prompts.

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

After installation, you will find the main executable at /usr/bin/varnishd and configuration files at /etc/varnish/. Here are the other key files you need to note.

  • /etc/varnish/varnish.params – varnish environment configuration file.
  • /etc/varnish/default.vcl – main varnish configuration file, written using vanish configuration language(VCL).
  • /etc/varnish/secret – varnish secret file

You can get the above information by running the following command in terminal.

# rpm -qi varnish

Verify installation by running the following commands

# which varnishd
# varnishd -V


6. Configure NGINX to Work with Varnish

By default, NGINX listens to port 80. In this step, we will make Varnish listen to port 80 and NGINX listen to port 8080 so that NGINX is running behind Varnish cache.

Open NGINX configuration file at /etc/nginx/nginx.conf

# vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g, such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command

# vi /etc/nginx/sites-enabled/website.conf

Look for the line in server block

listen 80;

and change it to

listen 8080;

Here’s an example

configure nginx listening port


Please note, if you make changes to a virtual host configuration file, only that website will be configured to work with Varnish cache.


7. Configure Varnish to Work with NGINX

By default, Varnish listens to port 6081. We need to make it listen to port 80. Open varnish configuration file.

# vi /etc/varnish/varnish.params

Update VARNISH_LISTEN_PORT from 6081 to 80 as shown below.

Configure Varnish Cache


8. Setup NGINX as backend proxy

Open /etc/varnish/default.vcl file to configure NGINX as backend proxy.

# vi /etc/varnish/default.vcl

Update the .port value in backend block to have 8080 value. This will point varnish cache to NGINX.

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


9. Restart NGINX and Varnish Cache

Restart NGINX and Varnish cache to apply changes

# systemctl restart nginx
# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish


10. Test Varnish Cache with NGINX

Run the curl command to send a request to your server.

# curl -I http://your_server_or_ip

You will see via : 1.1 varnish (Varnish /5.2) header

Test Varnish Cache on Nginx


Hopefully, now you can install and setup varnish cache for NGINX on CentOS, Redhat, Fedora Linux systems.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!


About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!