Assume : domain to be accessed is accessible via the url domain.com
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 sourceforge.net
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:
Code:
DAEMON_OPTS=”-a :80
-T localhost:6082
-f /usr/local/etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,200M
And do
Code:
/etc/rc.d/init.d/varnish start
If its not available under services, start it as below:
Code:
varnishd -f /usr/local/etc/varnish/default.vcl -T 127.0.0.1:6082 -a 0.0.0.0:80 -s malloc,200M
If you want to connec to the commandline interface use
varnishd -f /usr/local/etc/varnish/default.vcl -T 127.0.0.1:6082 -a 0.0.0.0:80 -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 http://www.varnish-cache.org/trac/wiki/Introduction
Code:
backend default {
.host = “69.50.217.14”;
.port = “8080”;
}acl purge {
“localhost”;
“69.50.217.14”;
}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;
return(lookup);
}// 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 ” && req.http.host == ” req.http.host);
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”;
return(pass);
}// 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”;
return(pass);
}// 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”;
}return(deliver);
}
7. Varnish Cache log can be started using the command below:
Code:
/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 http://domain.com 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:
Code:
ab -n1000 -c100 http://domain.com/