centos 7.5 部署varnish缓存服务器功能
一、varnish简介
Varnish是高性能开源的反向代理服务器和HTTP缓存服务器,其功能与Squid服务器相似,都可以用来做HTTP缓存。可以安装varnish在任何web前端,同时配置它缓存内容。与传统的squid相比,varnish具有性能更高、速度更快、管理更加方便等诸多优点。有一部分企业已经在生产环境中使用其作为旧版本的squid的替代方案,以在相同的服务器成本下提供更好的缓存效果,Varnish更是作为CDN缓存服务器的可选服务之一。
varnish的主要特性如下:
缓存位置:可以使用内存也可以使用磁盘。如果要使用磁盘的话推荐SSD做RAID1;
日志存储:日志也存储在内存中。存储策略:固定大小,循环使用;
支持虚拟内存的使用;
有精确的时间管理机制,即缓存的时间属性控制;
状态引擎架构:在不同的引擎上完成对不同的缓存和代理数据进行处理。可以通过特定的配置语言设计不同的控制语句,以决定数据在不同位置以不同方式缓存,在特定的地方对经过的报文进行特定规则的处理;
缓存管理:以二叉堆格式管理缓存数据,做到数据的及时清理。
varnish与squid相比,都是一个反向代理缓存服务器,并且都是开源的,varnish的稳定性很高,并且访问速度很快,因为Squid是从硬盘读取缓存的数据,而Varnish把数据存放在内存中,直接从读取内存,避免了频繁在内存、磁盘中交换文件,所以Varnish要相对更高效,varnish可以支持更多的并发连接,因为varnish的TCP连接释放要比squid快;varnish也可以通过管理端口,使用正则表达式批量的清除部分缓存,而squid是做不到的;squid属于是单进程使用单核CPU,但Varnish是通过fork形式打开多进程来做处理,所以可以合理的使用所有核来处理相应的请求。
上述说了很多varnish的优点,但是varnish也并非完美,其缺点主要有以下两个:
1、varnish进程一旦Crash或者重启,缓存数据都会从内存中完全释放,此时所有请求都会
发送到后端服务器,在高并发情况下,会给后端服务器造成很大压力;
2、在varnish使用中如果单个url的请求通过HA/F5等负载均衡,则每次请求落在不同的varnish服务器中,造成请求都会被穿透到后端;而且同样的请求在多台服务器上缓存,也会造成varnish的缓存的资源浪费,造成性能下降;
Varnish劣势的解决方案::
针对劣势一:在访问量很大的情况下推荐使用varnish的内存缓存方式启动,而且后面需要跟多台squid/nginx服务器。主要为了防止前面的varnish服务、服务器被重启的情况下,大量请求穿透varnish,这样squid/nginx可以就担当第二层CACHE,而且也弥补了varnish缓存在内存中重启都会释放的问题;
针对劣势二:可以在负载均衡上做url哈希,让单个url请求固定请求到一台varnish服务器上;
二、Varnish如何工作
Varnish的master进程负责启动工作,master进程读取配置文件,根据指定的空间大小(例如管理员分配了2G内存)来创建存储空间,创建并管理child进程;
然后child进程来处理后续任务,它会分配一些线程来执行不同的工作,例如:接受http请求、为缓存对象分配存储空间、清除过期缓存对象、释放空间、碎片整理等。
http请求处理过程如下:
1、有一个专门负责接收http请求的线程,一直监听请求端口,当有请求过来时,负责唤起一个工作线程来处理请求,工作线程会分析http请求的uri,知道了这个请求想要什么,就到缓存中查找是否有这个对象,如果有,就把缓存对象直接返回给用户,如果没有,会把请求转给后端服务器处理,并等待结果,工作线程从后端得到结果内容后,先把内容作为一个缓存对象保存到缓存空间(以备下次请求这个对象时快速响应),然后再把内容返回给用户
分配缓存过程如下:
有一个对象需要缓存时,根据这个对象的大小,到空闲缓存区中查找大小最适合的空闲块,找到后就把这个对象放进去,如果这个对象没有填满这个空闲块,就把剩余的空间做为一个新的空闲块,如果空闲缓存区中没地方了,就要先删除一部分缓存来腾出地方,删除是根据最近最少使用原则。
释放缓存过程如下:
有一个线程来负责缓存的释放工作,他定期检查缓存中所有对象的生存周期,如果某个对象在指定的时间段内没有被访问,就把这个对象删除,释放其占用的缓存空间,释放空间后,检查一下临近的内存空间是否是空闲的,如果是,就整合为一个更大的空闲块,实现空间碎片的整理。
更多varnish特性,请移步至varnish官方网站。
三、部署varnish缓存服务器
环境准备:
三台centos7.5服务器,IP分别为192.168.20.5、20.4、20.3;
其中IP192.168.20.5为varnish缓存服务器,而另外两台为后端web服务器,分别准备不同的网页文件(我这里将其网页内容更改为其IP),以便验证其缓存效果;
下载我提供的varnish源码包,并上传至varnish服务器。
1、开始部署安装varnish:
[root@varnish~]#wget-O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7.repo
#下载阿里镜像站的repo文件
[root@varnish~]#yum-yinstalllibedit-develpcre-develpython-docutils#安装依赖包
[root@varnish~]#cd/usr/src#切换至指定目录
[root@varnishsrc]#rz#上传我提供的varnish源码包
[root@varnishsrc]#tarzxfvarnish-4.0.3.tar.gz#解包
[root@varnishsrc]#cdvarnish-4.0.3/#进入解压后的目录
[root@varnishvarnish-4.0.3]#./configure&&make&&makeinstall#编译安装
[root@varnishvarnish-4.0.3]#cpetc/example.vcl/usr/local/var/varnish/#复制模板到指定路径
[root@varnishvarnish-4.0.3]#cd!$#切换到varnish安装目录下
[root@varnishvarnish]#vimexample.vcl#编辑其模板配置文件,根据自己所需功能,编写以下内容:
#
#ThisisanexampleVCLfileforVarnish.
#
#Itdoesnotdoanythingbydefault,delegatingcontroltothe
#builtinVCL.ThebuiltinVCLiscalledwhenthereisnoexplicit
#returnstatement.
#
#SeetheVCLchaptersintheUsersGuideathttps://www.varnish-cache.org/docs/
#andhttp://varnish-cache.org/trac/wiki/VCLExamplesformoreexamples.
#MarkertotelltheVCLcompilerthatthisVCLhasbeenadaptedtothe
#new4.0format.
vcl4.0;
importdirectors;
importstd;
#Defaultbackenddefinition.Setthistopointtoyourcontentserver.
probebackend_healthcheck{
.url="/";#访问后端服务器根路径
.interval=5s;#请求时间间隔
.timeout=1s;#请求超时时间
.window=5;#指定轮询次数5次
.threshold=3;#如果出现3次失败则表示后端服务器宕机
}
backendweb1{#定义后端服务器
.host="192.168.20.4";#要转向主机(即后端主机)的IP或域名
.port="80";#指定后端服务器的端口号
.probe=backend_healthcheck;#健康检查调用backend_healthcheck定义的内容
}
backendweb2{
.host="192.168.20.3";
.port="80";
.probe=backend_healthcheck;
}
aclpurgers{#定义访问控制列表
"127.0.0.1";
"localhost";
"192.168.20.0/24";
!"192.168.20.4";
}
subvcl_init{#调用vcl_init初始化子程序创建后端主机组,即directors
newweb_cluster=directors.round_robin();#使用new关键字创建drector对象,使用round_robin(轮询)算法
web_cluster.add_backend(web1);#添加后端服务器节点
web_cluster.add_backend(web2);
}
subvcl_recv{
setreq.backend_hint=web_cluster.backend();#指定请求的后端节点web_cluster定义的后端节点
if(req.method=="PURGE"){#判断客户端的请求头部是否是PURGE
if(!client.ip~purgers){#如果是,再判断客户端的IP地址是不是在ACL访问控制列表中.
return(synth(405,"NotAllowed."));#如果不是,返回给客户端405状态码并且返回定义的页面.
}
return(purge);#如果是ACL定义的,则交给purge处理.
}
if(req.method!="GET"&&
req.method!="HEAD"&&
req.method!="PUT"&&
req.method!="POST"&&
req.method!="TRACE"&&
req.method!="OPTIONS"&&
req.method!="PATCH"&&
req.method!="DELETE"){#判断客户端的请求类型
return(pipe);
}
if(req.method!="GET"&&req.method!="HEAD"){
return(pass);#如果不是GET及HEAD则交给pass.
}
if(req.url~"\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)"){
return(pass);#当客户端访问的是.php等结尾的交给pass处理.
}
if(req.http.Authorization){
return(pass);#当客户端请求的页面类型是需要认证的,交给pass处理
}
if(req.http.Accept-Encoding){
if(req.url~"\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$"){
unsetreq.http.Accept-Encoding;#取消客户端接收的压缩类型
}elseif(req.http.Accept-Encoding~"gzip"){
setreq.http.Accept-Encoding="gzip";#如果有gzip类型,标记gzip类型.
}elseif(req.http.Accept-Encoding~"deflate"){
setreq.http.Accept-Encoding="deflate";
}else{
unsetreq.http.Accept-Encoding;#其他未定义的页面也取消客户但接收的压缩类型.
}
}
if(req.url~"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)"){
unsetreq.http.cookie;#取消客户端的cookie值.
return(hash);#将请求转发给hash子程序,也就是查看本地缓存.
}
if(req.restarts==0){#判断客户端是不是第一次请求
if(req.http.X-Forwarded-For){#如果是第一次请求,设置获取客户端的IP地址.
setreq.http.X-Forwarded-For=req.http.X-Forwarded-For+","+client.ip;
}else{
setreq.http.X-Forwarded-For=client.ip;
}
}
return(hash);
}
subvcl_hash{
hash_data(req.url);#查看客户端请求的页面,并且进行hash
if(req.http.host){
hash_data(req.http.host);#设置客户端的主机
}else{
hash_data(server.ip);#设置服务器的IP
}
return(lookup);
}
subvcl_hit{
if(req.method=="PURGE"){#如果是HIT并且当客户端请求的类型是PURGE返回的200的状态码,并返回相应页面.
return(synth(200,"Purged."));
}
return(deliver);
}
subvcl_miss{
if(req.method=="PURGE"){
return(synth(404,"Purged."));#如果是miss返回404
}
return(fetch);
}
subvcl_deliver{
if(obj.hits>0){
setresp.http.CXK="HIT-from-varnish";#设置http头部X-Cache=hit
setresp.http.X-Cache-Hits=obj.hits;#返回命令的次数
}else{
setresp.http.X-Cache="MISS";
}
unsetresp.http.X-Powered-By;#取消显示web版本
unsetresp.http.Server;#取消显示varnish服务
unsetresp.http.X-Drupal-Cache;#取消显示缓存的框架
unsetresp.http.Via;#取消显示文件内容来源
unsetresp.http.Link;#取消显示HTML的超链接地址
unsetresp.http.X-Varnish;#取消显示varnish的id
setresp.http.xx_restarts_count=req.restarts;#设置客户端请求的次数
setresp.http.xx_Age=resp.http.Age;#显示缓存文件的时长
#setresp.http.hit_count=obj.hits;#显示缓存命中的次数
#unsetresp.http.Age;
return(deliver);
}
subvcl_pass{
return(fetch);#将后端服务器返回的数据缓存到本地
}
subvcl_backend_response{
setberesp.grace=5m;#缓存额外宽限时间
if(beresp.status==499||beresp.status==404||beresp.status==502){
setberesp.uncacheable=true;#当后端服务器相应状态码是449等,不缓存
}
if(bereq.url~"\.(php|jsp)(\?|$)"){
setberesp.uncacheable=true;#当是PHP的页面不缓存
}else{
if(bereq.url~"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)"){
setberesp.ttl=15m;#当是上面结尾的,缓存15分钟
unsetberesp.http.Set-Cookie;
}elseif(bereq.url~"\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)"){
setberesp.ttl=30m;#缓存30分钟
unsetberesp.http.Set-Cookie;
}else{
setberesp.ttl=10m;#生存时间10分钟
unsetberesp.http.Set-Cookie;
}
}
return(deliver);
}
subvcl_purge{
return(synth(200,"success"));
}
subvcl_backend_error{
if(beresp.status==500||
beresp.status==501||
beresp.status==502||
beresp.status==503||
beresp.status==504){
return(retry);#如果状态码是上述其中之一,则重新请求
}
}
subvcl_fini{
return(ok);
}
#编辑完成后,保存退出后即可。
[root@varnishvarnish]#varnishd-f/usr/local/var/varnish/example.vcl-smalloc,200M-a0.0.0.0:80
#启动varnish服务,监听本机所有IP的80端口,-f为指定vcl文件,-s是指定用来存放缓存的容量
[root@varnish~]#varnishlog#varnish启动后,可以执行此命令查看其日志。
客户端访问进行测试某些功能(谷歌浏览器访问前按“F12”):
按“F5”刷新一下:
访问到的是我们配置文件中指定的头部信息,并且状态码为304。
验证ACL清除缓存配置:
在主机192.168.20.4进行清除缓存操作(varnish配置的是不允许此IP清除缓存):
[root@localhost~]#curl-X"PURGE"192.168.20.5#清除varnish的缓存
会得到以下报错信息:
在varnish允许的IP上进行清除缓存操作(192.168.20.3主机),会看到以下成功的信息:
附加:
上述完整无注释的配置文件如下:
vcl4.0;
importdirectors;
importstd;
probebackend_healthcheck{
.url="/";
.interval=5s;
.timeout=1s;
.window=5;
.threshold=3;
}
backendweb1{
.host="192.168.20.4";
.port="80";
.probe=backend_healthcheck;
}
backendweb2{
.host="192.168.20.3";
.port="80";
.probe=backend_healthcheck;
}
aclpurgers{
"127.0.0.1";
"localhost";
"192.168.20.0/24";
!"192.168.20.4";
}
subvcl_init{
newweb_cluster=directors.round_robin();
web_cluster.add_backend(web1);
web_cluster.add_backend(web2);
}
subvcl_recv{
setreq.backend_hint=web_cluster.backend();
if(req.method=="PURGE"){
if(!client.ip~purgers){
return(synth(405,"NotAllowed."));
}
return(purge);
}
if(req.method!="GET"&&
req.method!="HEAD"&&
req.method!="PUT"&&
req.method!="POST"&&
req.method!="TRACE"&&
req.method!="OPTIONS"&&
req.method!="PATCH"&&
req.method!="DELETE"){
return(pipe);
}
if(req.method!="GET"&&req.method!="HEAD"){
return(pass);
}
if(req.url~"\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)"){
return(pass);
}
if(req.http.Authorization){
return(pass);
}
if(req.http.Accept-Encoding){
if(req.url~"\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$"){
unsetreq.http.Accept-Encoding;
}elseif(req.http.Accept-Encoding~"gzip"){
setreq.http.Accept-Encoding="gzip";
}elseif(req.http.Accept-Encoding~"deflate"){
setreq.http.Accept-Encoding="deflate";
}else{
unsetreq.http.Accept-Encoding;
}
}
if(req.url~"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)"){
unsetreq.http.cookie;
return(hash);
}
if(req.restarts==0){
if(req.http.X-Forwarded-For){
setreq.http.X-Forwarded-For=req.http.X-Forwarded-For+","+client.ip;
}else{
setreq.http.X-Forwarded-For=client.ip;
}
}
return(hash);
}
subvcl_hash{
hash_data(req.url);
if(req.http.host){
hash_data(req.http.host);
}else{
hash_data(server.ip);
}
return(lookup);
}
subvcl_hit{
if(req.method=="PURGE"){
return(synth(200,"Purged."));
}
return(deliver);
}
subvcl_miss{
if(req.method=="PURGE"){
return(synth(404,"Purged."));
}
return(fetch);
}
subvcl_deliver{
if(obj.hits>0){
setresp.http.CXK="HIT-from-varnish";
setresp.http.X-Cache-Hits=obj.hits;
}else{
setresp.http.X-Cache="MISS";
}
unsetresp.http.X-Powered-By;
unsetresp.http.Server;
unsetresp.http.X-Drupal-Cache;
unsetresp.http.Via;
unsetresp.http.Link;
unsetresp.http.X-Varnish;
setresp.http.xx_restarts_count=req.restarts;
setresp.http.xx_Age=resp.http.Age;
#setresp.http.hit_count=obj.hits;
#unsetresp.http.Age;
return(deliver);
}
subvcl_pass{
return(fetch);
}
subvcl_backend_response{
setberesp.grace=5m;
if(beresp.status==499||beresp.status==404||beresp.status==502){
setberesp.uncacheable=true;
}
if(bereq.url~"\.(php|jsp)(\?|$)"){
setberesp.uncacheable=true;
}else{
if(bereq.url~"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)"){
setberesp.ttl=15m;
unsetberesp.http.Set-Cookie;
}elseif(bereq.url~"\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)"){
setberesp.ttl=30m;
unsetberesp.http.Set-Cookie;
}else{
setberesp.ttl=10m;
unsetberesp.http.Set-Cookie;
}
}
return(deliver);
}
subvcl_purge{
return(synth(200,"success"));
}
subvcl_backend_error{
if(beresp.status==500||
beresp.status==501||
beresp.status==502||
beresp.status==503||
beresp.status==504){
return(retry);
}
}
subvcl_fini{
return(ok);
}
其实,若想实现varnish的缓存功能,通过以下基本的定义即可实现(example.vcl文件中有以下内容即可):
vcl4.0;
importdirectors;
probebackend_healthcheck{
.url="/";
.timeout=1s;
.interval=5s;
.window=5;
.threshold=3;
}
backendweb1{
.host="192.168.20.3";
.port="80";
.probe=backend_healthcheck;
}
backendweb2{
.host="192.168.20.4";
.port="80";
.probe=backend_healthcheck;
}
subvcl_init{
newweb_cluster=directors.round_robin();
web_cluster.add_backend(web1);
web_cluster.add_backend(web2);
}
subvcl_recv{
setreq.backend_hint=web_cluster.backend();
}
总结
以上所述是小编给大家介绍的centos7.5部署varnish缓存服务器功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。