Assume : domain to be accessed is accessible via the url
Varnish caching engine will run infront of Apache in port 80, and internally forward the requests to Apache.

Installation of Varnish:

1. Download Varnish source file from
2. Install libraries below via yum, as well as any other dependencies.

yum install logrotate libgomp gcc cpp binutils kernel-headers glibc-headers glibc-devel pcre-devel

3. Do ./configure, make, make install to install varnish
4. Configure Apache to listen on another port 8080 and restart Apache for changes to reflect.
5. Start Varnish as below, before that you can configure the default config file /usr/local/etc/varnish/default.vcl as mentioned in (6)
If varnish available under services, configure the file below with the parameters below:


-T localhost:6082
-f /usr/local/etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,200M

And do


/etc/rc.d/init.d/varnish start

If its not available under services, start it as below:


varnishd -f /usr/local/etc/varnish/default.vcl -T -a -s malloc,200M

If you want to connec to the commandline interface use

varnishd -f /usr/local/etc/varnish/default.vcl -T -a -s malloc,200M -d

6. A sample config file for Apache is as below. It can be tweaked as per customization required as outlined in


backend default {
.host = “”;
.port = “8080”;

acl purge {

sub vcl_recv {

// Strip cookies for static files:
if (req.url ~ “.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$”) {
unset req.http.Cookie;

// Remove has_js and Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, “(^|;s*)(__[a-z]+|has_js)=[^;]*”, “”);

// Remove a “;” prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, “^;s*”, “”);

// Remove empty cookies.
if (req.http.Cookie ~ “^s*$”) {
unset req.http.Cookie;

if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
purge(“req.url ~ ” req.url ” && == ”;
error 200 “Purged.”;

sub vcl_hash {
if (req.http.Cookie) {
set req.hash += req.http.Cookie;

sub vcl_fetch {

// Strip cookies for static files:
if (req.url ~ “.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$”) {
unset beresp.http.set-cookie;

// Varnish determined the object was not cacheable
if (!beresp.cacheable) {
set beresp.http.X-Cacheable = “NO:Not Cacheable”;

// You don’t wish to cache content for logged in users
elsif(req.http.Cookie ~”(UserID|_session)”) {
set beresp.http.X-Cacheable = “NO:Got Session”;

// You are respecting the Cache-Control=private header from the backend
elsif ( beresp.http.Cache-Control ~ “private”) {
set beresp.http.X-Cacheable = “NO:Cache-Control=private”;

// You are extending the lifetime of the object artificially
elsif ( beresp.ttl < 1s ) {
set beresp.ttl = 300s;
set beresp.grace = 300s;
set beresp.http.X-Cacheable = “YES:Forced”;

// Varnish determined the object was cacheable
else {
set beresp.http.X-Cacheable = “YES”;


7. Varnish Cache log can be started using the command below:


/usr/local/bin/varnishlog -w /tmp/vlog &

And tailing /tmp/vlog to see the dump of the requests handled by Varnish.

8. Any other problems with Apache can be checked from Apache error log, for instance shows a blank page.
9. You can also benchmark the difference in performance before and after using Varnish using the Apache bench marking tool ab and the commandline below:


ab -n1000 -c100

Contact Us On WhatsApp