配置varnish实现CDN加速功能
配置varnish实现CDN加速功能
Varnish进程的工作模式
Varnish启动会产生两个进程
manger主(管理mangement)进程
fork一个work子进程
Manger进程的作用
读入(更新配置),vcl文件编译
Varnish监控,初始化varnish及提供varnish管理接口
Management进程会每个几秒嗅探一下Child进程以判断其是否正常运行,
如果再指定的时长内未得到Child进程的回应,Management会将重启此Child进程
Vcl是基于C语言的
[root@server1 ~]# ps -ef
UID PID(进程号) PPID(父进程号) C STIME TTY TIME CMD
root 1 0 0 15:36 ? 00:00:00 /usr/lib/systemd/systemd --switc
root 2 0 0 15:36 ? 00:00:00 [kthreadd]
……
varnish 2154 1 0 16:18 ? 00:00:00 /usr/sbin/varnishd -a :80 -T loc
varnish 2164 2154 0 16:18 ? 00:00:00 /usr/sbin/varnishd -a :80 -T loc
可以看出第二个varnish进程是第一个varnish进程的子进程
Varnish的加速
修改varnish配置文件配置sub vcl_deliver模块
我们可以在它的请求响应报文里改
sub vcl_deliver模块:在缓存数据将要发送到用户端时调用
[root@server1 ~]# vim /etc/varnish/default.vcl
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-Cahce = "HIT from westos-huige.cache";
}
else {
set resp.http.X-Cahce = "MISS from westos cache";
}
return (deliver);
}
[root@server1 ~]# systemctl restart varnish
[root@foundation21 kiosk]# curl -I 172.25.21.1
HTTP/1.1 200 OK
……
X-Cahce: MISS from westos cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation21 kiosk]# curl -I 172.25.21.1
HTTP/1.1 200 OK
……
X-Cahce: HIT from westos-huige.cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation21 kiosk]# curl -I 172.25.21.1
HTTP/1.1 200 OK
……
X-Cahce: HIT from westos-huige.cache
Accept-Ranges: bytes
Connection: keep-alive
我们发现第一次不成功时MISS,后面都是HIT(命中)
它的默认缓存时间是120s
清除varnish缓存
手动(varnish adm的组合命令
varnishadm ban req.url "~" /
#ban :清理缓存中满足表达式的缓存对象
我们一般使用于域名进行通信
测试:
[root@server1 ~]# varnishadm ban req.url "~" /
[root@foundation21 kiosk]# curl -I 172.25.21.1
HTTP/1.1 200 OK
……
X-Cahce: MISS from westos cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation21 kiosk]# curl -I 172.25.21.1
HTTP/1.1 200 OK
……
X-Cahce: HIT from westos-huige.cache
Accept-Ranges: bytes
Connection: keep-alive
手动清理成功
[root@foundation21 kiosk]# curl -I 172.25.21.1/index.html
HTTP/1.1 200 OK
……
X-Cahce: HIT from westos-huige.cache
Accept-Ranges: bytes
Connection: keep-alive
#手动清除页面缓存
[root@server1 ~]# varnishadm ban req.url "~" /index.html
[root@foundation21 kiosk]# curl -I 172.25.21.1/index.html
HTTP/1.1 200 OK
……
X-Cahce: MISS from westos cache
Accept-Ranges: bytes
Connection: keep-alive
配置default.vcl加速配置
[root@server1 ~]# vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.21.2";
.port = "80";
}
backend web2 {
.host = "172.25.21.3";
.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.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web1;
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
}else {
return (synth(405));
}
}
// An highlighted block
var foo = 'bar';
给真机做解析
[root@foundation21 kiosk]# vim /etc/hosts
172.25.21.1 www.westos.org bbs.westos.org
测试
[root@foundation21 kiosk]# curl www.westos.org
server2
[root@foundation21 kiosk]# curl bbs.westos.org
server3