Sunday, October 21, 2012

Varnish Cache for Centos.

 Varnish Cache for Centos.

Add EPEL repository to Yum

#rpm -ivh http://mirror.nus.edu.sg/Fedora/epel//5/i386/epel-release-5-4.noarch.rpm

# yum install jemalloc-2.1.3-2.el5.x86_64.rpm

#rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
#yum install varnish

Now to edit the varnish configuration file. It’s located at: etc/varnish/default.vcl. Insert the following code in the file. Explanation of the different constructs follows:


======================================================================================
backend default {
  .host = "your.primary.domainname.com";
  .port = "80";
}
sub vcl_recv {
  if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\?.*|)$") {
      remove req.http.Accept-Encoding;
    } elsif (req.http.Accept-Encoding ~ "gzip") {
      set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
      set req.http.Accept-Encoding = "deflate";
    } else {
      remove req.http.Accept-Encoding;
    }
  }
  if (req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
    unset req.http.cookie;
  }
  if (req.request == "GET" && req.url ~ "cron_job" ||         req.url ~ "something_else"        ) {
    return ( pass );
  }
}
======================================================================================



To start the varnish process 
 


# varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:6081 -a 0.0.0.0:80



To redirect http traffic to varnish port 
 =========================
 iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 6081

 =========================

No comments:

Post a Comment