Varnish是一款強大的反向代理加速軟體,關於其工作原理可以參考上圖,其具體流程及VCL語法我這裡就不做說明,網上資料多,大家還可以對照參考其官方網站和《Varnish中文權威指南》。
Varnish中文權威指南 PDF 下載
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2013年資料/9月/10日/CentOS 5.8下Varnish-2.1.5的安裝配置
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
相關閱讀:
利用Varnish構建Cache伺服器筆記 http://www.linuxidc.com/Linux/2012-07/65234.htm
Varnish 編譯安裝所需準備 http://www.linuxidc.com/Linux/2012-07/65230.htm
緩存服務Varnish安裝配置 http://www.linuxidc.com/Linux/2012-07/65228.htm
利用Varnish和Nginx來使用WebSocket http://www.linuxidc.com/Linux/2012-05/61214.htm
Linux下Varnish緩存的配置優化 http://www.linuxidc.com/Linux/2012-03/56435.htm
一、安裝CentOS5.8系統環境下的依耐關係
12 yum install gcc gcc-c++
yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool -y
二、下載varnish-2.1.5源碼包,並進行編譯安裝。
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命令是用來檢查軟體的依耐關係是否滿足,如果報錯的話, 則應該如下
正常所示:
+ aclocal
+ libtoolize --copy --force
+ autoheader
+ automake --add-missing --copy --foreign
+ autoconf
繼續編譯安裝:
12 ./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings
make && make install && cd ../
三、創建varnish用戶和組,以及varnish緩存文件和日誌存放目錄:
/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}
四、我的測試環境是兩台Web機器,IP為192.168.1.103(域名為http://www.linuxidc.net)的varnish機器對後端IP為192.168.1.104和192.168.1.105的機器進行反向代理加速,其配置文件/usr/local/varnish/etc/varnish/better.vcl如下所示:
backend rserver1
{
.host ="192.168.1.104";
.port = "80";
.probe = {
.timeout = 5s; #等待多長時間超時
.interval = 2s; #檢查時間間隔
.window = 10; #varnish將維持10個sliding windows的結果
.threshold = 8; #如果是8次.windows檢查是成功的,就宣告後端的Web機器
是健康的
}
}
backend rserver2
{
.host ="192.168.1.105";
.port = "80";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 10;
.threshold = 8;
}
}
#指定一個名為realserver組,使用random機制,權重越大,分配的訪問越多,可根據
伺服器性能來設定;而round-robin(輪詢)機制是不能指定weight的
director realserver random {
{
.backend = rserver1;
.weight = 5;
}
{
.backend = rserver2;
.weight = 6;
}
}
#定義能清理緩存的機器,這裡只允許本機能用purge的方式清理
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv
{
if (req.http.host ~"^(.*).linuxidc.net")
{
set req.backend =realserver;
}
else
{
error 200 "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;
}
#對HTTP協議中的GET、HEAD請求進行緩存,對POST請求透過,讓其直接訪問後端Web服
務器。之所以這樣配置,是因為POST請求一般是發送數據給伺服器的,需要伺服器接
收、處理,所以不緩存;
if (req.request !="GET" && req.request != "HEAD")
{
return (pipe);
}
if (req.http.Expect)
{
return (pipe);
}
if (req.http.Authenticate|| req.http.Cookie)
{
return (pass);
}
if (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 +=server.ip;
}
return (hash);
}sub vcl_hit
{
if (req.request =="PURGE")
{
set obj.ttl = 0s;
error 200"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 503 "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.linuxidc.net";
} else {
set resp.http.X-Cache= "MISS FROM www.linuxidc.net";
}
return (deliver);
}
[火星人 ] CentOS 5.8下Varnish-2.1.5的安裝配置已經有476次圍觀