Varnish is a powerful reverse agent accelerator software, about its working principle can refer to the above diagram, its specific flow and VCL syntax I do not do here, the information on the Internet, you can also refer to its official website and the "Varnish Chinese authoritative guide."
First, the installation of CentOS5.8 system environment in accordance with the resistance relationship
Yum Install gcc gcc-c++
Yum install Automake autoconflibtool ncurses-devel libxslt Groff pcre-devel pkgconfig libtool-y
Second, download the varnish-2.1.5 source package, and compile the installation.
Cd/usr/local/src
wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
Tar zxvf varnish-2.1.5.tar.gz
CD varnish-2.1.5.
./autogen.sh
#autogen. SH command is used to check whether the software depends on the resistance of the relationship, if the error, it should be as follows
Normal as shown:
+ aclocal
+ libtoolize--copy--force
+ Autoheader
+ automake--add-missing--copy--foreign
+ autoconf
To continue compiling the installation:
./configure--prefix=/usr/local/varnish--enable-dependency-tracking--enable-debugging-symbols-- Enable-developer-warnings-enable-extra-warnings
Make && make install && CDs. /
Third, create varnish users and groups, and varnish cache files and log storage directory:
/usr/sbin/groupadd Varnish
/usr/sbin/useradd-s/sbin/nologin-g Varnish Varnish
Mkdir-p/data/varnish/{cache,log}
Chown-r Varnish:varnish/data/varnish/{cache,log}
Four, my test environment is two web machines, IP for 192.168.1.103 (domain name for http:// www.yuhongchun027.net) Varnish machine Reverse proxy acceleration for 192.168.1.104 and 192.168.1.105 machines, and its configuration file/usr/local/varnish/etc/ The VARNISH/BETTER.VCL is as follows:
Backend Rserver1 {. host = "192.168.1.104";. Port = "n";. Probe = {. Timeout = 5s; #等待多长时间超时. interval = 2s; #检查时间间隔. window = 10; #varnish将维持10个sliding the results of windows. Threshold = 8; #如果是8次. Windows check is successful, declaring that the backend Web machine is healthy} backend Rserver2 {. host = "192.168.1.105";. Port = "";. Probe = {. Timeout = 5
S
. interval = 2s;
. window = 10;
. threshold = 8; } #指定一个名为realserver组, using the random mechanism, the greater the weight, the more allocated access can be set based on server performance, while the Round-robin (polling) mechanism is unable to specify weight director Realserver
Random {. backend = Rserver1;. Weight = 5;}
{. backend = Rserver2;. Weight = 6;}
#定义能清理缓存的机器, only this function is allowed to clean ACL purge {"localhost" in purge way;
"127.0.0.1";
Sub Vcl_recv {if (req.http.host ~ "^ (. *). Yuhongchun027.net") {set req.backend =realserver;
else {error "Nocahce for this domain";
} if (req.request = = "PURGE") {if (!client.ip ~purge) { Error 405 "not allowed.";}
else {return (pipe); } #获取客户端真实IP地址 if (req.http.x-forwarded-for) {Set req.http.x-forwarded-for = Req.http.x-forwarded-for "
, "CLIENT.IP;
else {set req.http.x-forwarded-for =client.ip; The #对HTTP协议中的GET, head request is cached, and the POST request is accessed to direct access to the back-end Web server. This configuration is because post requests typically send data to the server, requiring the server to receive, process, and therefore not cache; if (req.request!= "get" && req.request!= "Head") {Retur
n (pipe); } if (Req.http.Expect) {return (pipe),} if (req.http.authenticate| | req.http.Cookie) {return (pass);} I F (req.http.cache-control~ "No-cache") {return (pass);} #对JSP或者PHP文件不缓存 if (req.url ~ "\.jsp" | | req.url ~ "\.php" {return (pass);} else {return (lookup)}} Sub Vcl_pipe {return (pipe);} Sub Vcl_pass {return (pass);} Sub Vcl_hash {set Req.hash + = Req.url; if (req.http.host) {set Req.hash +=req.http.host;} else {set Req.hash +=serv
Er.ip;
} return (hash);
}sub Vcl_hit {if (req.request = = "PURGE") {set obj.ttl = 0s;
Error "purged.";}
if (!obj.cacheable) {return [pass];} return (deliver); }sub Vcl_miss {if (req.request = = "PURGE") {error 404 "not Incache.";} if (req.http.user-agent ~ "Spider") {Error 5
"notpresently in Cache";
return (fetch); Sub Vcl_fetch {if (req.request = = "Get" && req.url ~ "\." TXT|JS) $ "{Set beresp.ttl = 3600s;} else {set beresp.ttl = 30d;} if (!beresp.cacheable) {return (pass);} if (
Beresp.http.set-cookie) {return [pass];} return (deliver);
Sub Vcl_deliver {if (obj.hits > 0) {set resp.http.x-cache= "HIT from Www.yuhongchun027.net";
else {set resp.http.x-cache= "MISS from Www.yuhongchun027.net";
return (deliver); }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/