高命中率的varnish缓存配置分享
2023-06-13 09:15:38 时间
给大家一个连我这么丑的blog,命中率都可以达到75%的varnish配置,大家拿去根据自己网站情况再优化下的话,说不定也可以达到90%,这也不是不可能的事。
系统:centos5.x
软件:varnish-3.0.5
1.安装varnish
怎么安装我就不说了吧,自己搜去。
2.varnish配置
backendslogra{
.host="172.0.0.1";
.port="80";
.connect_timeout=20s;
.first_byte_timeout=20s;
.between_bytes_timeout=20s;
}
#允许刷新缓存的规则
aclpurgeAllow{
# 只能本机进行刷新
"localhost";
}
#Belowisacommented-outcopyofthedefaultVCLlogic. Ifyou
#redefineanyofthesesubroutines,thebuilt-inlogicwillbe
#appendedtoyourcode.
subvcl_recv{
#判断请求主机,跳转到相应后端服务器
if(req.http.host~"^(.*)(slogra.com)")
{
setreq.backend=slogra;
}else{
error408"Hostnamenotfound";
}
#grace缓存过期仍存放
#若backend是健康的,则仅grace5s,如果backend不健康,则grace1m。
#这里,5s的目的是为了提高高并发时的吞吐率;
#1m的目的是,backend挂了之后,还能继续服务一段时间,期望backend挂的不要太久。。。
if(req.backend.healthy){
setreq.grace=5s;
}else{
setreq.grace=1m;
}
#刷新缓存的处理
if(req.request=="PURGE"){
if(!client.ip~purgeAllow){
error405"Notallowed.";
}
# #转到hit或者miss处理
return(lookup);
}
#移除一些特定格式的cookie
if(req.url~"^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)"){
#移除cookie,以便能缓存到varnish
unsetreq.http.cookie;
}
#Accept-Encoding是浏览器发给服务器,声明浏览器支持的编码类型的
#修正客户端的Accept-Encoding头信息
#防止个别浏览器发送类似deflate,gzip
if(req.http.Accept-Encoding){
if(req.url~"^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)"){
removereq.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";
}elseif(req.http.Accept-Encoding~"sdch"){
#chrome新增加的压缩
setreq.http.Accept-Encoding="sdch";
}else{
removereq.http.Accept-Encoding;
}
}
#首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端ip
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;
}
}
if(req.request!="GET"&&
req.request!="HEAD"&&
req.request!="PUT"&&
req.request!="POST"&&
req.request!="TRACE"&&
req.request!="OPTIONS"&&
req.request!="DELETE"){
return(pipe);
}
if(req.request!="GET"&&req.request!="HEAD"){
/*WeonlydealwithGETandHEADbydefault*/
return(pass);
}
if(req.http.Authorization){
/*Notcacheablebydefault*/
return(pass);
}
#js,css文件都有Cookie,不能每次都去后台服务器去取
#if(req.http.Cookie){
# /*Notcacheablebydefault*/
# return(pass);
#}
#如果请求的是动态页面直接转发到后端服务器
if(req.url~"^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)"){
return(pass);
}
return(lookup);
}
subvcl_pipe{
#Notethatonlythefirstrequesttothebackendwillhave
#X-Forwarded-Forset. IfyouuseX-Forwarded-Forandwantto
#haveitsetforallrequests,makesuretohave:
#setbereq.http.connection="close";
#here. Itisnotsetbydefaultasitmightbreaksomebrokenweb
#applications,likeIISwithNTLMauthentication.
return(pipe);
}
#放过,让其直接去后台服务器请求数据
subvcl_pass{
return(pass);
}
subvcl_hash{
hash_data(req.url);
if(req.http.host){
hash_data(req.http.host);
}else{
hash_data(server.ip);
}
#支持压缩的要增加,防止发送给不支持压缩的浏览器压缩的内容
if(req.http.Accept-Encoding){
hash_data(req.http.Accept-Encoding);
}
return(hash);
}
#缓存服务器lookup查找命中:hit
subvcl_hit{
#刷新缓存的请求操作,设置TTL为0,返回处理结果代码
if(req.request=="PURGE"){
setobj.ttl=0s;
error200"Purged.";
}
#缓存服务器命中后(查找到了)
return(deliver);
}
#缓存服务器lookup查找没有命中:miss
subvcl_miss{
#刷新缓存的请求操作,
#if(req.request=="PURGE"){
# error404"Notincache.";
#}
#缓存服务器没有命中(去后台服务器取)
return(fetch);
}
#从后台服务器取回数据后,视情况是否进行缓存
subvcl_fetch{
#如果请求的是动态页面直接发转发
#动态请求回来的,一定要放在前面处理
if(req.url~"^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)"){
setberesp.http.Cache-Control="no-cache,no-store";
unsetberesp.http.Expires;
return(deliver);
}
#仅当该请求可以缓存时,才设置beresp.grace,若该请求不能被缓存,则不设置beresp.grace
if(beresp.ttl>0s){
setberesp.grace=1m;
}
if(beresp.ttl<=0s||
beresp.http.Set-Cookie||
beresp.http.Vary=="*"){
/*
*Markas"Hit-For-Pass"forthenext2minutes
*/
setberesp.ttl=120s;
#下次请求时不进行lookup,直接pass
return(hit_for_pass);
}
#设置从后台服务器获得的特定格式文件的缓存TTL
if(req.url~"^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")
{
#移除服务器发送的cookie
unsetberesp.http.Set-Cookie;
#加上缓存时间
setberesp.ttl=30d;
return(deliver);
}elseif(req.url~"^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){
#移除服务器发送的cookie
unsetberesp.http.Set-Cookie;
#加上缓存时间
setberesp.ttl=15d;
return(deliver);
}elseif(req.url~"^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){
#移除服务器发送的cookie
unsetberesp.http.Set-Cookie;
#加上缓存时间
setberesp.ttl=30d;
return(deliver);
}
#从后台服务器返回的response信息中,没有缓存的,不缓存
if(beresp.http.Pragma~"no-cache"||beresp.http.Cache-Control~"no-cache"||beresp.http.Cache-Control~"private"){
return(deliver);
}
return(deliver);
}
#缓存服务器发送到客户端前调用
subvcl_deliver{
#下面是添加一个Header标识,以判断缓存是否命中。
if(obj.hits>0){
setresp.http.X-Cache="HITfromcache";
#setresp.http.X-Varnish="HITfromcache";
}else{
setresp.http.X-Cache="MISSfromcache";
#setresp.http.X-Varnish="MISSfromcache";
}
#去掉不是必须的header
unsetresp.http.Vary;
unsetresp.http.X-Powered-By;
unsetresp.http.X-AspNet-Version;
return(deliver);
}
subvcl_error{
setobj.http.Content-Type="text/html;charset=utf-8";
setobj.http.Retry-After="5";
synthetic{"
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"}+obj.status+""+obj.response+{"</title>
</head>
<body>
<h1>Error"}+obj.status+""+obj.response+{"</h1>
<p>"}+obj.response+{"</p>
<h3>GuruMeditation:</h3>
<p>XID:"}+req.xid+{"</p>
<hr>
<p>cacheserver</p>
</body>
</html>
"};
return(deliver);
}
subvcl_init{
return(ok);
}
subvcl_fini{
return(ok);
}
3.效果图
好了,大家有兴趣的,可以自己去搞.
相关文章
- nginx配置proxy_cache缓存策略
- 免费的WordPress超级缓存插件与配置教程
- 关于window10安装jdk,配置环境变量,javac不是内部或外部命令,也不是可运行的程序 或批处理文件的细节问题。[通俗易懂]
- 记一次 Druid 超时配置的问题 → 引发对 Druid 时间配置项的探究
- webpack5 配置多线程和缓存加快构建速度
- Spring Cloud 整合 nacos 实现动态配置中心
- 配置中心config-client
- 【Android Gradle 插件】AaptOptions 配置 ④ (failOnMissingConfigEntry 配置|ignoreAssets/ignoreAssetsPattern配置)
- Windows下MongoDB的下载安装、环境配置教程图解
- mysql高并发配置详解数据库
- 分布式缓存系统之Memcached安装配置详解大数据
- Oracle 参数 PERMIT_92_WRAP_FORMAT 官方解释,作用,如何配置最优化建议
- Ehcache缓存配置详解编程语言
- 配置Linux服务器双网卡配置指南(linux服务器双网卡)
- 优化:Redis缓存配置实战(redis缓存配置)
- 如何优化Redis缓存配置(redis缓存配置)
- 优化网站性能,实用技能:Redis配置缓存详解(redis配置缓存)
- Linux系统下简易代理配置指南(linux代理配置)
- Redis网址配置指南(怎样配置redis的网址)
- TP5新高度使用Redis缓存极大提升应用性能(tp5配置redis缓存)
- SSM框架如何配置Redis缓存(ssm怎么配置redis)
- 有效使用Oracle下载安装及注册码配置(oracle下载注册码)
- Redis调优配置提升性能的最佳选择(redis 调优配置)
- 简单而高效Redis缓存配置实践(redis缓存配置数据)
- 使用Redis缓存系统提升性能配置篇(redis缓存系统配置)
- CentOS4.0安装配置Nginx的方法
- squid3缓存服务器编译安装和高命中率配置示例