天天看點

RHEL6.4安裝varnish

varnish開源項目是2006年釋出的第一個版本0.9.距今已經八年多了,此文檔之前也提過varnish還不穩定,那是2007年時候編寫的,現在的varnish已經很健壯。很多門戶網站已經部署了varnish,并且反應都很好,甚至反應比squid還穩定,且效率更高,資源占用更少。相信在反向代理,web加速方面,varnish已經有足夠能力代替squid。

Varnish 的作者Poul-Henning Kamp是FreeBSD(BSD UNIX分支的開發者)的核心開發者之一,他認為現在的計算機比起1975年已經複雜許多。在1975年時,儲存媒介隻有兩種:記憶體與硬碟。但現在計算機系統的記憶體除了主存外,還包括了CPU内的L1、L2,甚至有L3快取。硬碟上也有自己的快取裝置,是以Squid Cache自行處理物件替換的架構不可能得知這些情況而做到最佳化,但作業系統可以得知這些情況,是以這部份的工作應該交給作業系統處理,這就是 Varnish cache設計架構。

一個典型的例子:

挪威大的線上報紙 Verdens Gang 使用3台Varnish代替了原來的12台Squid,性能比以前更好

下邊進行varnish的安裝

varnish官網:https://www.varnish-cache.org

varnish下載下傳:https://www.varnish-cache.org/releases

varnish支援的平台(作業系統)有:

  • Debian
  • FreeBSD
  • Red Hat Enterprise Linux 5/6
  • Ubuntu

此處以RHEL6為準測試

varnish已經對RHEL&CentOS支援很好,支援線上yum安裝指令如下:

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

rpm直接遠端安裝這個包會生成yum源的配置檔案

#

yum install varnish

#即可安裝varnish chache server

此處我的機器不能連接配接到網際網路,所有采用源碼包安裝

源碼包下載下傳位址:http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz

RPM包下載下傳位址:http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/

下邊先說源碼包的安裝:

[[email protected] ~]# cd /usr/src/ && tar -zxvf varnish-3.0.3.tar.gz && cd varnish-3.0.3       #将下載下傳的包mv到 /usr/src然後解包再進入varnish的安裝目錄

[[email protected] varnish-3.0.3]# yum -y install gcc glib gcc-c++ pcre-devel       #安裝依賴包為了相容正規表達式

[[email protected] varnish-3.0.3]# ./configure --prefix=/usr/local/varnish       #編譯安裝的配置

[[email protected] varnish-3.0.3]# make && make install                                #編譯和安裝

至此安裝完成,下邊簡單叙述一下rpm包的安裝過程:

擷取包

# wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-agent-1.16-0.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-debuginfo-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-docs-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-libs-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-libs-devel-3.0.4-1.el6.x86_64.rpm

以上包包含動态連結庫包,文檔包,開發依賴包等等

[[email protected] RPMvarnish]# rpm -ivh varnish-debuginfo-3.0.4-1.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:varnish-debuginfo      ########################################### [100%]

[[email protected] RPMvarnish]# rpm -ivh varnish-docs-3.0.4-1.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:varnish-docs           ########################################### [100%]

[[email protected] RPMvarnish]# rpm -ivh varnish-libs-3.0.4-1.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:varnish-libs           ########################################### [100%]

[[email protected] RPMvarnish]# rpm -ivh varnish-libs-devel-3.0.4-1.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:varnish-libs-devel     ########################################### [100%]

[[email protected] RPMvarnish]#varnish-agent-1.16-0.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:varnish-libs-devel     ########################################### [100%]

如果支援連接配接到網絡的話建議用如下方式安裝:

For RHEL 6 and compatible distributions, use

     rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release-3.0-1.el6.noarch.rpm

and then run

yum install varnish

The

--no-signature

is only needed on initial installation, since the Varnish GPG key is not yet in the yum keyring

下邊進行varnish的啟動介紹和簡單的配置:

簡單的配置varnish:

[[email protected] varnish-3.0.3]# cat /etc/varnish/default.vcl

backend webserver {

     .host = "127.0.0.1";                          //後端伺服器的IP位址或者完全的FQDN名或者hostname

     .port = "8000";                                       //後端伺服器提供服務的端口

 }

sub vcl_recv {

        if (!req.http.x-forwarded-for) {

                set req.http.X-Forwarded-For = client.ip;

        }

            if (req.request != "GET" &&

               req.request != "HEAD" &&

               req.request != "PUT" &&

               req.request != "POST" &&

               req.request != "TRACE" &&

               req.request != "OPTIONS" &&

               req.request != "DELETE") {

                   return (pipe);

            }

       if (req.request != "GET" && req.request != "HEAD") {

               return (pass);

        }

        if (req.url ~ "\.(jsp|do)($|\?)") {

            return (pass);

        }

    else {

            return (lookup);

        }

}

sub vcl_pipe {

        return (pipe);

}

sub vcl_pass {

        return (pass);

}

sub vcl_hash {

    hash_data(req.url);

    if (req.http.host) {

        hash_data(req.http.host);

    } else {

        hash_data(server.ip);

    }

    return (hash);

}

sub vcl_hit {

            return (deliver);

}

sub vcl_miss {

        return (fetch);

}

sub vcl_fetch {

     if (beresp.ttl <= 0s ||

        beresp.http.Set-Cookie ||

        beresp.http.Vary == "*") {

               set beresp.ttl = 120 s;

               return (hit_for_pass);

     }

    if (req.request == "GET" && req.url ~ "^/static(.*)$") {

       set beresp.ttl = 600s;

    }

    if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {

       set beresp.ttl = 1800s;

    }

    return (deliver);

}

sub vcl_deliver {

        if (obj.hits > 0) {

               set resp.http.X-Cache = "HIT john.local";

        }

    else {

              set resp.http.X-Cache = "MISS from john.local";

        }

        return (deliver);

}

****************************************

這是一個簡單的示例性配置檔案筆者

沒有作出詳細的注釋或者解釋,筆者

将會在後邊的博文中漸漸解釋VCL也

就是varnish的配置檔案的寫法

****************************************

啟動varnish服務:

[[email protected] varnish-3.0.3]# /usr/local/varnish/sbin/varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80

參數說明:

f: 需要加載的vcl檔案

s:用malloc函數動态排程記憶體,為1G

T:管理端口和監聽位址

a:服務的監聽端口,對外提同服務

簡單的測試:

[[email protected] varnish-3.0.3]# lsof -i:80

COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

varnishd 13467 nobody    6u  IPv4  25286      0t0  TCP *:http (LISTEN)

[[email protected] varnish-3.0.3]# curl -I http://192.168.0.111

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Sun, 11 Aug 2013 11:57:24 GMT

ETag: "248c-1b-4e3aab609ddb4"

Content-Type: text/html; charset=UTF-8

Content-Length: 27

Accept-Ranges: bytes

Date: Sat, 31 Aug 2013 15:00:45 GMT

X-Varnish: 2074431089 2074431086

Age: 48

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT john.local                        //HIT為命中緩存

[[email protected] ~]# curl -I http://192.168.0.111

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Sun, 11 Aug 2013 11:57:24 GMT

ETag: "248c-1b-4e3aab609ddb4"

Content-Type: text/html; charset=UTF-8

Content-Length: 27

Accept-Ranges: bytes

Date: Sat, 31 Aug 2013 15:02:43 GMT

X-Varnish: 2074431090

Age: 0

Via: 1.1 varnish

Connection: keep-alive

X-Cache: MISS from john.local                  //MISS為未命中

VCL寫法将會在下篇博文中詳細剖析

VCL詳解 請移步至:

http://blog.csdn.net/cwg_1992/article/details/10742625

########################################

本文有筆者原創

作者:john

轉載請注明出處