Varnish is a state-of-the-art, high-performance HTTP accelerator. It uses the advanced features in Linux 2.6, FreeBSD 6/7 and Solaris 10 to achieve its high performance.
Some of the features include
- A modern design
- VCL – a very flexible configuration language
- Load balance with health checking of backends
- Partial support for ESI
- URL rewriting
- Graceful handling of "dead" backends
INSTALL:
1.download varnish-2.0.4.tar.gz
If you want the last version,you can hit here.
2.unzip varnish
tar zxvf varnish-2.0.4.tar.gz
3.compile varnish
cd varnish-2.0.4
./configure –prefix=/usr/local/varnish –enable-debugging-symbols –enable-developer-warnings –enable-dependency-tracking –enable-tests –enable-diagnostics
make
make install
4.create user:
adduser -s /sbin/nologin www
5.create cache directory & log file
mkdir -p /var/vcache && chown www.www /var/vcache/
mkdir -p /usr/local/varnish/var/logs && chown www.www /usr/local/varnish/var/logs
6.create config file:
vcl.conf
backend photo1 {
.host = "10.0.5.33";
.port = "80";
.probe = {
.url = "/guide.php";
.timeout = 50ms;
.interval = 5s;
.window = 10;
.threshold = 8;
}
}
backend photo2 {
.host = "10.0.7.33";
.port = "80";
.probe = {
.url = "/guide.php";
.timeout = 50ms;
.interval = 5s;
.window = 10;
.threshold = 8;
}
}
director photo_director round-robin {
{ .backend = photo1; }
{ .backend = photo2; }
}
acl purge {
"localhost";
"127.0.0.1";
"10.0.0.0"/16;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (client.ip ~ purge) {
purge_url(req.url);
}
else{
error 200 "Purged";
}
}
if (req.url ~ "\.(gif|jpg|jpeg|bmp)$") {
unset req.http.cookie;
unset req.http.authenticate;
set req.backend = photo_director;
}
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
deliver;
}
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
set req.hash += req.http.cookie;
hash;
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}
sub vcl_fetch {
if (!obj.cacheable) {
pass;
}
if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
pass;
}
deliver;
}
7.Optimize the kernel parameters:
net.ipv4.ip_local_port_range = 1024 65536
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.ipv4.tcp_fin_timeout = 3
net.ipv4.tcp_tw_recycle = 1
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save=1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
8.Start varnish:
#!/bin/sh
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl-php.conf -a 0.0.0.0:80 -s malloc -g www -u www -w 500,10000,15 -T 127.0.0.1:3500 -p client_http11=on -plisten_depth=4096 -p lru_interval=3600 -h classic,500009 -p obj_workspace=2048
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /usr/local/varnish/var/logs/varnish.log &
[...] Accelerate your web site to visit with varnish [...]