歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> varnish-安裝使用

varnish-安裝使用

日期:2017/2/27 15:49:00   编辑:Linux教程

以centos為例安裝varnish4

具體可以參考官方鏈接 https://www.varnish-cache.org/installation/redhat

# 安裝varnish repo
rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
# 安裝varnish
yum install varnish

由於牆的原因,如果不能安裝varnish repo,可以直接通過翻牆下載varnish的rpm來直接安裝varnish (我是直接使用的紅杏chrome插件一個月10塊,挺不錯,我不是托兒)

rpm地址:http://repo.varnish-cache.org/redhat/varnish-4.0/el6/x86_64/varnish/

varnish-libs-devel-4.0.3-1.el6.x86_64.rpm
varnish-libs-4.0.3-1.el6.x86_64.rpm
varnish-docs-4.0.3-1.el6.x86_64.rpm
varnish-debuginfo-4.0.3-1.el6.x86_64.rpm
varnish-4.0.3-1.el6.x86_64.rpm

具體包的作用可以通過名字來反映出,注意安裝順序。

相關文件:

/etc/varnish/               vcl配置文件默認目錄。
/etc/sysconfig/varnish      varnish配置文件
/etc/sysconfig/varnishncsa  varnish ncsa日志配置文件
/var/log/varnish/           默認varnish日志存放目錄
/etc/init.d/varnish         varnish啟動腳本
/etc/init.d/varnishlog      varnish debug日志啟動腳本
/etc/init.d/varnishncsa     varnish ncsa日志啟動腳本

可用命令:

varnishadm          varnish管理接口CLI
varnishd            varnish daemon
varnishhist         varnish柱狀顯示圖程序 
varnishlog          varnish debug日志命令
varnishncsa         varnish ncsa日志命令
varnish_reload_vcl  varnish reload vcl命令
varnishstat         varnish狀態查看命令
varnishtest         測試varnish程序的命令
varnishtop          varnish 日志排名

具體每個命令的解釋,如果安裝了varnish-docs-4.0.3-1.el6.x86_64.rpm 都可以通過man來查看,建議安裝。

配置varnish

關於varnish的配置主要兩個

/etc/sysconfig/varnish      關於varnishd運行參數的配置
/etc/varnish/default.vcl    vcl配置文件,如何處理http請求

varnish啟動腳本調用/etc/sysconfig/varnish,每項參數在該配置文件中均有說明

vim /etc/sysconfig/varnish

# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#

# Maximum number of open files (for ulimit -n)
NFILES=131072
# 如果ulimit -n的值小於NFILES ,以內核參數為准,如果大於,以NFILES為准

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"

# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

# Set this to 1 to make init script reload try to switch vcl without restart.
# To make this work, you need to set the following variables
# explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,
# VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1
# 值為1時,可以通過調用/etc/init.d/varnish reload來重載vcl。

# This file contains 4 alternatives, please use only one.

## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080.  Use a fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -b localhost:8080 \
#             -u varnish -g varnish \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -f /etc/varnish/default.vcl \
#             -u varnish -g varnish \
#             -S /etc/varnish/secret \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # Main configuration file. You probably want to change it :)
VARNISH_VCL_CONF=/etc/varnish/default.vcl
# 指定加載默認的vcl配置文件的路徑

#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
# 監聽地址,如果沒有指定,將監聽在所有接口上
VARNISH_LISTEN_PORT=80
# 監聽端口

#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=192.168.11.208
VARNISH_ADMIN_LISTEN_PORT=6082
# 管理監聽地址和端口

#
# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=50
# 伴隨varnishd啟動是每個work啟動的線程數量,如果大量請求,請將此值調大,以避免啟動後的短暫瞬間線程數量不夠導致的鏈接丟失。

#
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000
# 最大work啟動線程數量,
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120
# 如果threads在120秒處於空閒狀態,將會被varnish回收掉,但最終threads的值也不會小於VARNISH_MIN_THREADS定義值

#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=500M
# 指定varnishd用來存放cache的空間。

#
# # Backend storage specification
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
# 指定varnish的存儲類型,可選file、malloc
#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
            -f ${VARNISH_VCL_CONF} \
            -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
            -t ${VARNISH_TTL} \
            -p thread_pool_min=${VARNISH_MIN_THREADS} \
            -p thread_pool_max=${VARNISH_MAX_THREADS} \
            -p thread_pool_timeout=${VARNISH_THREAD_TIMEOUT} \
            -u varnish -g varnish \
            -S ${VARNISH_SECRET_FILE} \
            -s ${VARNISH_STORAGE}"
# 將上面的變量引用到DAEMON_OPTS參數,varnishd啟動時會應用DAEMON_OPTS
#


## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
# DAEMON_OPTS=""

vim /etc/varnish/default.vcl

# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
# 指定從哪裡可以取得資源。這樣一個簡單的vanish配置就完成了
backend default {
    .host = "192.168.11.208";
    .port = "81";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
    if (bereq.url ~ "\.(js|css|png|jpg|ico)$" && beresp.ttl <= 1200s && beresp.status == 200) {
        unset beresp.http.set-cookie;
        unset beresp.http.Cache-Control;
        unset beresp.http.Via;
        set beresp.ttl = 1200s;
    }
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    # 
    # You can do accounting or modifying the final object here.
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from varnish "+obj.hits;
    } 
    else {
        set resp.http.X-Cache = "MISS from varnish "+obj.hits;
    }
    return (deliver);
}

啟動varnish

/etc/init.d/varnish start

這時通過訪問你的網頁,js/css/png/jpg/ico的資源將會被緩存,如果你需要緩存其他資源可以在vclbackendresponse中定義,ttl指定緩存的時間,如果不指定默認讀取/etc/sysconfig/varnish中的TTL的值120秒。

通過查看http的響應頭將發現

Accept-Ranges:bytes
Age:73
Connection:keep-alive
Content-Length:2040
Content-Type:text/css
Date:Fri, 05 Jun 2015 08:35:36 GMT
ETag:"55715980-7f8"
Last-Modified:Fri, 05 Jun 2015 08:10:40 GMT
Server:openresty
Via:1.1 varnish-v4
X-Cache:HIT from varnish 496
X-Varnish:30314557 10948617

特別注意的是:在某些條件下varnish將不緩存內容,最常見的是使用cookie。當一個被標記有cookie的客戶端web請求,varnish默認是不緩存。這些眾多的varnish功能特點都是可以通過寫vcl來改變的。

sub vcl_recv {
        # Remove the cookie header to enable caching
        unset req.http.cookie;
}

最 後提個建議,如果要使用varnish緩存html,這時你需要考慮的是,是否包含cookie,已經後台的更新(包括評論管理,文章修改,屏蔽等等)如 何通知varnish等等,需要你結合你的應用來添加vcl配置參數。典型的便是在發起後台更新操作時在vcl配置文件中截獲文章的id,然後更新緩存。

提供一個參考例子:

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    # html先清除cookie
    if (req.method == "GET" && req.url ~ "^/articles/.*\.html$" && req.http.referer !~ "console" ) {
        unset req.http.Cookie;
        unset req.http.Cache-Control;
        return(hash);
    }

    # 評論更新
    if (req.method == "POST" && req.url ~ "/reviews"  ) {
        set req.http.purge-url = regsub(req.http.referer,"http://www.example.com.cn/articles/","");
        ban("req.url ~ " + req.http.purge-url);
        set req.backend_hint = default;
        return(pass);
    }

    # 文章更新
    if (req.method == "POST" && req.url ~ "/console/articles/\d+" ) {
        set req.http.purge-url = regsub(req.url,"/console/articles/(\d+).*","\1");
        ban("req.url ~ " + req.http.purge-url + ".html$");
        set req.backend_hint = default;
        return(pass);
    }

    # 為發往後端主機的請求添加X-Forward-For首部
    if (req.http.X-Forward-For) {    
        set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip;
    } else {
        set req.http.X-Forward-For = client.ip;
    }

    if (req.http.host ~ "static.example.com.cn") {
        set req.backend_hint = static;
        return (hash);
    } elsif (req.http.host ~ "image(\d)*.example.com.cn") {
        set req.backend_hint = image;
        return (hash);
    } elsif (req.http.host ~ "www.example.com.cn" && req.url ~ "\.(png|jpg)$") {
        set req.backend_hint = default;
        return(hash);
    } else {
        set req.backend_hint = default;
    }

}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers

    if (bereq.url ~ "\.(html)$" && beresp.ttl <= 600s && beresp.status == 200 &&  beresp.http.set-cookie !~ "staff_id") {
        unset beresp.http.set-cookie;
        unset beresp.http.Cache-Control;
        unset beresp.http.Via;
        set beresp.ttl = 600s;
    }

    if (bereq.url ~ "\.(js|css|png|jpg|ico)$" && beresp.ttl <= 1200s && beresp.status == 200) {
        unset beresp.http.set-cookie;
        unset beresp.http.Cache-Control;
        unset beresp.http.Via;
        set beresp.ttl = 1200s;
    }
}
Copyright © Linux教程網 All Rights Reserved