1. 程式人生 > >varnish4雜記(1)

varnish4雜記(1)

varnish4 基礎 雜記

varnish4基礎應用

varnish4使用

1、安裝varnish4

[root@master1 ~]# yum install -y varnish

1.1 內存回收模塊

Name        : jemalloc
Arch        : x86_64
Version     : 3.6.0
Release     : 1.el7
Size        : 317 k
Repo        : installed
From repo   : ali-epel
Summary     : General-purpose scalable concurrent malloc implementation
URL         : http://www.canonware.com/jemalloc/
License     : BSD
Description : General-purpose scalable concurrent malloc(3) implementation.
            : This distribution is the stand-alone "portable" implementation of
            : jemalloc.

[root@master1 ~]# 

1.2 查看服務啟動文件

[root@master1 ~]# cat /usr/lib/systemd/system/varnish.service

2、修改服務器啟動配置選項

[root@master1 ~]# vim /etc/varnish/varnish.params 

#VARNISH_STORAGE="malloc,256M"
VARNISH_STORAGE="malloc,256M"       #修改緩存方式

3、配置另一臺web服務器(節點2)

[root@master2 ~]# yum -y install httpd

配置測試頁:
[root@master2 ~]# for i in {1..10}; do echo "Page $i on Web1" > /var/www/html/test$i.html;done
[root@master2 ~]# ls /var/www/html/
test10.html  test2.html  test4.html  test6.html  test8.html
test1.html   test3.html  test5.html  test7.html  test9.html
[root@master2 ~]# 

啟動服務:
[root@master2 ~]# systemctl start httpd

設置開機啟動:
[root@master2 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@master2 ~]# 
[root@master2 ~]# systemctl is-enabled httpd
enabled
[root@master2 ~]# 

4、配置varnish的acl後端主機文件

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "10.201.106.132";       跳轉到web2
    .port = "80";
}

開啟varnish服務:
[root@master1 ~]# systemctl start varnish.service

varnish:6081,6082
[root@master1 ~]# ss -tnl
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
LISTEN      0      50                                                      *:3306                                                                *:*                  
LISTEN      0      128                                                     *:22                                                                  *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                                     *:6081                                                                *:*                  
LISTEN      0      10                                              127.0.0.1:6082                                                                *:*                  
LISTEN      0      128                                                    :::22                                                                 :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                    :::6081                                                               :::*                  
[root@master1 ~]# 

訪問測試:
http://10.201.106.131:6081/test9.html
正常

5、varnishadm

5.1 查看緩存命中結果

連進去:
[root@master1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 
200        
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-327.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-4.0.4 revision 386f712

Type ‘help‘ for command list.
Type ‘quit‘ to close CLI session.

ping
200        
PONG 1484619827 1.0
status
200        
Child in state running
vcl.list
200        
active          0 boot

vcl.load test default.vcl
200        
VCL compiled.
vcl.list
200        
active          0 boot
available       0 test

vcl.use test
200        
VCL ‘test‘ now active
vcl.use t
varnish> vcl .list
101        
Unknown request.
Type ‘help‘ for more info.

vcl.list
200        
active          0 boot
available       0 test

vcl.discard test
200        

vcl.list
200        
active          0 boot

5.2 查看當前參數值

Type ‘help‘ for more info.

param.show

設置參數值:
param.set thread_pools 4

顯示單個參數:
param.show thread_pools
200        
thread_pools
        Value is: 4 [pools]
        Default is: 2
        Minimum is: 1

查看進程上次掛掉原因
panic.show
300        
Child has not panicked or panic has been cleared

顯示正在使用的存儲:
varnish> storage.list
200        
Storage devices:
    storage.Transient = malloc
    storage.s0 = malloc

顯示配置文件編譯前的樣子:
vcl.show boot

查看後端服務器列表:
varnish> backend.list
200        
Backend name                   Refs   Admin      Probe
default(10.201.106.132,,80)    1      probe      Healthy (no probe)

列出定義的ban規則,(緩存對象清除規則)
varnish> ban.list
200        
Present bans:
1484618521.742461     0 C  

6、顯示varnish日誌

[root@master1 ~]# varnishlog 

7、另一種日誌格式

[root@master1 ~]# varnishncsa 
10.201.106.1 - - [17/Jan/2017:21:19:42 +0800] "GET http://10.201.106.131:6081/test1.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"

8、查看狀態和排序

[root@master1 ~]# varnishstat 

[root@master1 ~]# varnishtop

配置vcl

1、備份vcl,配置備份的vcl

[root@master1 ~]# cd /etc/varnish/
[root@master1 varnish]# cp default.vcl test.vcl
[root@master1 varnish]# vim test.vcl 

2、編輯配置文件

[root@master1 varnish]# vim test.vcl 

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.
    if (req.method == "PRI") {
        /* We do not support SPDY or HTTP/2.0 */
        return (synth(405));
    }
    if (req.method != "GET" &&
        req.method != "HEAD" &&
        req.method != "PUT" &&
        req.method != "POST" &&
        req.method != "TRACE" &&
        req.method != "OPTIONS" &&
        req.method != "DELETE") {
            /* Non-RFC2616 or CONNECT which is weird. */
            return (pipe);
    }

    if (req.method != "GET" && req.method != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
        /* Not cacheable by default */
        return (pass);
    }
    return (hash);
}

3、vcl裝載新配置文件

連入vcl:
[root@master1 varnish]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082

裝載配置文件:
vcl.load test1 test.vcl
200        
VCL compiled.

查看:
vcl.list
200        
active          0 boot
available       0 test1

使用test1,使其生效:
vcl.use test1
200        
VCL ‘test1‘ now active

查看test1配置:
vcl.show test1
200        
#
# 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.
backend default {
    .host = "10.201.106.132";
    .port = "80";
}

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.
    if (req.method == "PRI") {
    /* We do not support SPDY or HTTP/2.0 */
    return (synth(405));
    }
    if (req.method != "GET" &&
    req.method != "HEAD" &&
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }

    if (req.method != "GET" && req.method != "HEAD") {
    /* We only deal with GET and HEAD by default */
    return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
    /* Not cacheable by default */
    return (pass);
    }
    return (hash);
}

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.
}

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.
}

4、deliver測試

4.1 自定義命中響應內容

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";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

4.2 重新載入激活配置文件

載入文件
vcl.load test2 test.vcl
200        
VCL compiled.

查看配置文件列表
vcl.list
200        
available       0 boot
active          0 test1
available       0 test2

激活使用配置文件
vcl.use test2
200        
VCL ‘test2‘ now active

4.3 測試

curl測試:
[root@node1 ~]# curl http://10.201.106.131:6081/test3.html
Page 3 on Web1

curl只響應首部:
[root@node1 ~]# curl -I http://10.201.106.131:6081/test3.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:05:02 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 131079 131075
Age: 38
Via: 1.1 varnish-v4
X-Cache: HIT                            #已命中
Connection: keep-alive

[root@node1 ~]# 

4.4 第一次請求網頁MISS,第二次就HIT了

[root@node1 ~]# curl -I http://10.201.106.131:6081/test4.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:06:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 23
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS               ###
Connection: keep-alive

[root@node1 ~]# curl -I http://10.201.106.131:6081/test4.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:06:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 131081 24
Age: 6
Via: 1.1 varnish-v4
X-Cache: HIT                    ###
Connection: keep-alive

[root@node1 ~]# 

5、添加server.ip

5.1 修改配置

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 " + server.ip;
    } else {
        set resp.http.X-Cache = "MISS " + server.ip;
    }
}

5.2 裝載激活配置文件

vcl.load test3 test.vcl
200        
VCL compiled.
vcl.use test3
200        
VCL ‘test3‘ now active

5.3 測試

root@node1 ~]# curl -I http://10.201.106.131:6081/test1.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:41:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 26
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS 10.201.106.131            ###
Connection: keep-alive

[root@node1 ~]# curl -I http://10.201.106.131:6081/test1.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:41:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 163844 27
Age: 24
Via: 1.1 varnish-v4
X-Cache: HIT from 10.201.106.131            ###
Connection: keep-alive

[root@node1 ~]# 

varnish4雜記(1)