CentOSServer

How to install and configure Varnish 3 with Nginx 1.6 on Centos 6

Varnish is a great web application accelerator. Both varnish and nginx can be configured as a httpd cached proxy, but varnish is more advanced in caching (varnish can cache whole page both static and dynamic page), and nginx works better as a web server. Previously I wrote an tutorial How to install and configure Varnish with Apache on Centos 6, but if you want to increase Web Server Scalability, you should switch Apache for Nginx. So I’m make another tutorial how to install and configure Varnish 3 with Nginx 1.6 on Centos 6. I assume you already have a working LEMP (Linux, Nginx, MySQL and PHP-FPM), if not you can follow my another tutorial How to install LEMP web server with Nginx, PHP-FPM 5.5, MySQL 5.5 on Centos Linux.

As of the time I’m writing this tutorial, the latest Varnish Cache release is 4.0.0 but only available in Source version. There is no Debian or Redhat/Centos packages available yet so we are going to use Varnish release 3 for this tutorial.

Install Varnish on Centos 6 Nginx Web Server

Before you isntall Varnish, you will have to install Varnish’s dependencies

# yum update
# yum install gcc make automake autoconf libtool ncurses-devel libxslt groff pcre-devel pckgconfig libedit libedit-devel
To install Varnish 3 on RHEL 6, Centos 6, and compatible distributions
# rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm
# yum install varnish
To install Varnish 3 on RHEL 5, Centos 5, and compatible distributions
# rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.el5.centos.noarch.rpm
# yum install varnish
To start Varnish on your Centos 6
# service varnish start
To start Varnish automatically by default when the system boot/reboot
# chkconfig varnish on

Configure Varnish on Centos 6 Nginx Web Server

Default system wide varnish configuration file will be located at “/etc/sysconfig/varnish” and will come with four alternatives. The basic VLC configuration file is located at “/etc/varnish/default.vcl”.

We are going to modify varnish configuration file to make Varnish works in front of Nginx web server on Centos

# nano /etc/sysconfig/varnish

Alternative 3, Advanced configuration is enabled by default, but we are going to use Alternative 2 which is easier to configure. So we are going to disable Alternative 3 and enable Alternative 2. To disable Varnish Alternative 3 configuration, add dash sign (#) before every line in Alternative 3.

Now we are going to enable Alternative 2 by removing dash sign (#) start from “DAEMON_OPTS” line.

By default Varnish listen port 6081 front end. We are going to use Varnish front end and nginx back end. All we have to do is to change Varnish listening port to port 80.

Replace port 6081 to 80 in Alternative 2 from

DAEMON_OPTS="-a :6081 \
to
DAEMON_OPTS="-a :80 \
Now your Varnish Alternative 2 should look something like this. Save the file after you are done.
## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -u varnish -g varnish \
             -S /etc/varnish/secret \
             -s file,/var/lib/varnish/varnish_storage.bin,1G"
Next thing we are going to do is to configure the basic VLC configuration file /etc/varnish/default.vcl

Related Articles

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button