Install Varnish in front of Nginx Proxy web server in CentOS/RedHat

Why are we using Nginx, Varnish and PHP-FPM?

Nginx is a web-server, like Apache, but boosts much better performance results, and is much more stable when handling lots of requests, which results in high CPU usage and can slow down your entire server. Apache, for example, doesn’t scale quiet so well without a large amount of know-how, and in my experience I have found it easier to use an alternative web server, hence Nginx! Now, Nginx isn’t without it’s flaws, you will need to install PHP-FPM, which is a FastCGI Process Manager for PHP, essentially it allows us to use PHP via FastCGI, I believe their are alternative solutions to this, however this is one I have come across and is simple enough to setup.

So We can Install Varnish in front of Nginx proxy web server which makes cache for dynamic content and static content also for making faster our site.Varnish Cache is a HTTP proxy or sometimes referred to as a HTTP accelerator. What Varnish does is sit between you and your web-server, when a page is requested Varnish will check if it has a cached version and return it to the user, if not it simply passes the request onto the normal web server (in this case Nginx), it then caches it so in future it can be returned quickly. What’s even better about Varnish is it makes use of the physical memory on your server as opposed to the CPU, which results in high speed without slowing your server down. This leads to some seriously good performance results.

Before installation of Varnish first we need to setup nginx with php-fpm =

Steps to install varnish in front of Nginx Proxy web server

Following are the steps to install and configure Varnish in front of Apache Web Server for caching static content.

Get the RPM of Varnish

rpm --nosignature -i

Install the Varnish using yum. This step can be executed in Centos/Redhat/Fedora

yum install varnish

Configure Varnish

Now next we have to configure Varnish. This can be slightly different based on your CMS and/or framework and how your site is explicitly set up, but we will get you started with a basic configuration.

Once we have Apache and Varnish install on our server we have to configure Varnish on port 80 to serve the content while fetching it from apache which will run on port 8080.

Open below file with any editor

vim /etc/sysconfig/varnish

Uncomment all of the lines under “DAEMON_OPTS”—under Alternative 2, and make the configuration match the following code:

 DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

The above configuration will run Varnish on port 80 annd varnish admin on port 6082 with given .vcl file path.

Once you have saved this file with above configuration setup open default.vcl file and update configuration for backend of varnish for fetching content from apache.

vim /etc/varnish/default.vcl

Configuration should look like this –

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

This will setup backend on port 8080 for host on which apache will listen.

Configure Nginx

Although we have already configured varnish to expect that nginx ports will be running on 8080, the default settings for nginx are still on port 80. We will correct the discrepancy now.

Open up the virtual host file with the default information.

vi /etc/nginx/nginx.conf

The Virtual Host should also be set to port 8080 and be accessible only from the localhost. The updated line looks like this:

server {
        listen; ## listen for ipv4; this line is default and implied

Once you have made all of the required changes, restart varnish and nginx.

sudo service nginx restart
sudo service varnish restart

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish’s workings on your VPS with this command:


We also check the varnish logs and their cache hit or miss pages –



Linuxtweaks Blog helping Server Admin to Manage their servers, Desktop users for making more friendly with linux. Tutorials , guides and tips for linux server maintenance. Here you can learn how to tweak linux servers with code and how to manage it properly.

You may also like...

Leave a Reply

Your email address will not be published.