设为首页 收藏本站
查看: 778|回复: 0

[经验分享] 利用varnish构建httpd缓存服务器

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-6-22 09:47:11 | 显示全部楼层 |阅读模式
varnish如何存储缓存对象:
    file: 单个文件;不支持持久机制;
    malloc: 缓存在内存中;
    persistent:基于文件的持久存储;(此方式不建议使用)

vcl:配置缓存系统的缓存机制;【线程中缓存功能的工作机制】
一、在vs2和vs3上安装http
写入文件,内容一个为on vs2,另一个为on vs3
1
2
3
4
5
6
[iyunv@vs2 ~]# yum install http
[iyunv@vs2 ~]# for i in {1..10}; do echo "web$i on vs2" > /var/www/html/test$i.html; done
[iyunv@vs2 ~]# ls /var/www/html/
test10.html  test2.html  test4.html  test6.html  test8.html
test1.html   test3.html  test5.html  test7.html  test9.html
[iyunv@vs2 ~]# systemctl start httpd.service



二、安装varnish(在centos7上安装4.0.3版本)
1
[iyunv@vs1 ~]# yum install varnish



三、varnish主程序的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[iyunv@vs1 ~]# vim /etc/varnish/varnish.params

# Varnish environment configuration description. This was derived from
# the old style sysconfig/defaults settings

# Set this to 1 to make systemd reload try to switch vcl without restart.
RELOAD_VCL=1

# 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=192.168.1.5
VARNISH_LISTEN_PORT=6081    #监听的服务端口为6081

# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1    #监听的管理地址为本机
VARNISH_ADMIN_LISTEN_PORT=6082    #监听的管理端口为6082

# Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret    #密钥文件位置

# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"    #缓存以文件的方式的存储位置和大小
#VARNISH_STORAGE="malooc,256M"    #以内存方式缓存,缓存大小为256M

# Default TTL used when the backend does not specify one
VARNISH_TTL=120    #联系后端服务器超时时长

# User and group for the varnishd worker processes
VARNISH_USER=varnish    #主进程所使用的用户
VARNISH_GROUP=varnish    #主进程所使用的组

# Other options, see the man page varnishd(1)    #进程选项,线程池的最大值最小值和超时时长
DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300"




四、varnish的命令行管理工具
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@vs1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
help      
help [<command>]    #获取帮助信息
ping [<timestamp>]    #测试服务器是否正常
auth <response>    #
quit                #退出
banner
status            #显示服务器状态信息
start            #启动子进程
stop            #停止子进程
vcl.load <configname> <filename>    #载入哪个文件为配置文件
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname>                #使用哪个vcl文件
vcl.discard <configname>            #删除哪个vcl文件
vcl.list                     #列出所有可用的vcl文件
param.show [-l] [<param>]            #显示运行时参数
param.set <param> <value>               
panic.show                     #显示恐慌信息,显示进程或子进程上次挂掉的原因
panic.clear                                        #清除恐慌信息
storage.list                                        #显示缓存信息
vcl.show [-v] <configname>                        #显示vcl文件的详细信息,vcl编译前的样子
backend.list [<backend_expression>]                #显示后端服务器列表
backend.set_health <backend_expression> <state>    #手动上线下线后端服务器
ban <field> <operator> <arg> [&& <field> <oper> <arg>]...    #清理缓存中的缓存对象
ban.list                                             #显示定义的清理缓存规则



varnish的访问日志
1
2
[iyunv@vs1 ~]# varnishlog
[iyunv@vs1 ~]# varnishtop



varnish的统计信息
1
[iyunv@vs1 ~]# varnishstat



五、vcl配置文件的说明
http://book.varnish-software.com/4.0/chapters/VCL_Basics.html
http://book.varnish-software.com/4.0/_images/simplified_fsm.svg
1466011145365271.jpg
1
2
3
4
5
6
7
8
9
10
11
vcl_recv    接收请求
cacheable    判断是否为可缓存对象
incache        判断hash后的结果是否存在
vcl_hash    可缓存对象hash计算
vcl_hit    缓存中命中
vcl_miss    缓存中未命中
vcl_fetch    获取后端内容
vcl_deliver    构建缓存发送
vcl_pipe        客户端请求的方法不是常见方法时,直接交给后端服务器处理
vcl_pass        不检查缓存直接从后端服务器取
vcl_error    varnish直接返回错误响应



六、vcl配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
[iyunv@vs1 ~]# cp /etc/varnish/default.vcl /etc/varnish/test.vcl
[iyunv@vs1 ~]# vim /etc/varnish/test.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.
backend vs2 {    #定义后端主机vs2
    .host = "172.16.24.102";
    .port = "80";
    .probe = {    #对后端主机的test1进行进行健康状态检测
        .url = "/test1.html";
        }
}
backend vs3 {
    .host = "172.16.24.104";
    .port = "80";
    .probe = {
        .url = "/test1.html";
        }
}
#import directors;    #加载directors模块,在负载均衡轮询时要用到
#sub vcl_init {        #轮询方式的负载均衡
#    new mycluster = directors.round_robin();
#    mycluster.add_backend(vs2);
#    mycluster.add_backend(vs3);
#}

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.
     
    #url中开头带有login或者admin的直接从后端主机取结果不缓存
    if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin") {  
        return(pass);
    }

    #url以jpg,png,gif结尾的直接发给vs2,其他的都发给vs3
    if (req.url ~ "(?i)\.(jpg|png|gif)$") {
        set req.backend_hint = vs2;
    } else {
        set req.backend_hint = vs3;
    }


#    set req.backend_hint = mycluster.backend();    #负载均衡集群


#如果客户端请求为PRI返回405,如果请求的为非GET,HEAD,PUT,POST,TRACE,OPTIONS,DELETE都直接发给后端主机处理
    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.
    #如果缓存能命中就在返回值中插入HIT,未命中则插入MISS
    if (obj.hits>0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

[iyunv@vs1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test2 test.vcl
200        
VCL compiled.
vcl.use test2
200        
VCL 'test2' now active



七、测试
在vs2上上传一张dog.jpg,在vs3上不上传任何图片
1466017455468062.jpg
1466017477103436.jpg
1466017498592277.jpg
1466017528888636.jpg
1466019052650261.jpg
1466019125541130.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-233584-1-1.html 上篇帖子: CentOS下的sudo相关配置的总结归纳 下篇帖子: crontab计划任务默认存放的文件,以及修改方式 服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表