web快取服務器varnish-4.1.6的部署及配置詳解

來源:互聯網
上載者:User

標籤:jsp   分享   random   dha   檢測   方法   狀態   timeout   tar   

web快取服務器varnish-4.1.6的部署及配置詳解


1.安裝varnish4.1.6
安裝依賴

yum install -y autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx

安裝varnish yum倉庫

# rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.1.el7.rpm # yum install varnish -y

 

2.設定Varnish參數

# grep "^[a-Z]" /etc/varnish/varnish.params RELOAD_VCL=1    # 重新啟動服務時是否重新讀取VCL並重新編譯VARNISH_VCL_CONF=/etc/varnish/default.vcl    # 預設讀取的VCL檔案VARNISH_LISTEN_PORT=80    # 設定監聽的連接埠(預設監聽6081連接埠)VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1    # 管理介面監聽的地址VARNISH_ADMIN_LISTEN_PORT=6082    # 管理介面監聽的連接埠VARNISH_SECRET_FILE=/etc/varnish/secret    # 使用的密鑰檔案VARNISH_STORAGE="malloc,256M"    # 隱藏檔的大小VARNISH_USER=varnish    # varnish預設使用者VARNISH_GROUP=varnish    # varnish預設組

 

定義VCL backend

# cat /etc/varnish/default.vcl |grep -v "#"vcl 4.0;backend web01 {    .host = "192.168.3.12";    .port = "80";}

 

啟動varnish
# systemctl start varnish
# systemctl enable varnish

3.定義VCL 後端的集合 director
VCL 可以把多個 backends 彙總成一個組,這些組被叫做 director,這樣可以增強效能和彈力,當組裡一個 backend 掛掉後,可以選擇另一個健康的 backend。VCL 有多種 director,不同的 director 採用不同的演算法選擇 backend,主要有以下幾種:
a. The random director
Random director 會根據所設定的權值(weight)來選擇 backend,.retries 參數表示嘗試找到一個 backend 的最大次數,.weight 參數表示權值
b. The round-robin director
Round-robin director 在選擇 backend 時,會採用迴圈的方式依次選擇。
c. The client director
Client director 根據 client.identity 來選擇 backend,您可以設定 client.identity 的值為 session cookie 來標識 backend。

# vim /etc/varnish/default.vclvcl 4.0;                    # 指明varnish版本                        backend web01 {                    # 定義後端伺服器1    .host = "192.168.3.12";    .port = "80";}backend web02 {                    # 定義後端伺服器2    .host = "192.168.3.13";    .port = "80";}import directors;                # 定義directorssub vcl_init {                    # 定義vcl_init子常式        new cluster1 = directors.round_robin();        cluster1.add_backend(web01);        cluster1.add_backend(web02);}sub vcl_recv {                    # 定義vcl_recv子常式        set req.backend_hint = cluster1.backend(); #指定後端directors}

詳情:https://www.varnish-cache.org/docs/4.1/reference/vmod_directors.generated.html#object-hash

4.設定響應是否命中

sub vcl_deliver {                   # 定義子常式        if (obj.hits > 0) {                      set resp.http.X-Cache = "HIT via" + " " + server.ip;        } else {                set resp.http.X-Cache = "MISS via" + " " + server.ip;        }             # 判斷如果命中就在http響應首部設定X-Cache為HIT,否則就在http響應首部設定X-Cache為MISS。}

 

然後再到頁面上訪問看一下是否已經生效,可以看到第一次訪問的時候是MISS第二次的時候就是HIT了


5.指定某些檔案不能查緩衝

sub vcl_recv {        if (req.url ~ "^/test.html$") {                return(pass);        }            ##定義請求的檔案中如果匹配test.html就pass,不查緩衝}

 

通過測試可以看到連續訪問http://192.168.3.198/test.html都是MISS

6.進行健全狀態檢查
Varnish可以檢測後端主機的健康狀態,在判定後端主機失效時能自動將其從可用後端主機列表中移除,而一旦其重新變得可用還可以自動將其設定為可用。為了避免誤判,Varnish在探測後端主機的健康狀態發生轉變時(比如某次探測時某後端主機突然成為不可用狀態),通常需要連續執行幾次探測均為新狀態才將其標記為轉換後的狀態。
每個後端伺服器當前探測的健康狀態探測方法通過.probe進行設定,其結果可由req.backend.healthy變數擷取,也可通過varnishlog中的Backend_health查看或varnishadm的debug.health查看。

backend web01 {    .host = "192.168.10.132";    .port = "80";    .probe = {        .url = "/";            # 指定哪個url需要varnish請求        .timeout = 2s;         # 指定逾時等待時間        .interval = 5s;         # 指定檢查時間間隔        .window = 5;            # 最多嘗試5次        .threshold = 3;          # 至少有3次成功就宣告backend健康    }}backend web02 {    .host = "192.168.3.13";    .port = "80";    .probe = {        .url = "/";        .timeout = 2s;        .interval = 5s;        .window = 5;        .threshold = 3;    }}

 

7.設定緩衝時間長度

sub vcl_backend_response {    if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") {        set beresp.ttl = 2h;    }                 # 如果url是以圖片格式結尾的緩衝2小時    if (bereq.url ~ "\.(html|css|js|jsp)$") {        set beresp.ttl = 30m;    }                  # 如果url是以html|css|js|jsp結尾的緩衝30分鐘}

 

web快取服務器varnish-4.1.6的部署及配置詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.