Varnish跟Squid都是一款内容加速缓存服务器,我们可以使用它们来对我们的网页内容进行缓存,以此来从某个方面提高用户体验度,提升网站整体的抗压能力。

目前自建的CDN中,有很多都是基于Squid、Varnish等相关缓存软件,经过内部的二次开发实现了更好的内容加速及管理。

那今天我们一起来学习一下Varnish简单的搭建及日常的维护,深入的东西后期分享,跟大家一起来交流。这里直接上Shell脚本自动初始化并安装:

#!/bin/sh
#auto install varnish
#2014-07-28 by wugk
DIR=/usr/src
CODE=$?
VER=$1if  [ -z $1 ];thenecho "Usage:{$0 'Version' install |config |start|help } , Example $0 2.1.5 install}"exit
fiURL=https://repo.varnish-cache.org/source/varnish-${VER}.tar.gzfunction install()
{if  [ ! -d /usr/local/varnish -o ! -f /etc/init.d/varnishd ];thencd $DIR ;wget -c $URLif  [ $CODE == '0' ];thenecho "This varnish Files is Download Successed!"elseecho "This varnish Files is Download Failed!"fiuseradd  -s /sbin/noin varnishmkdir -p /data/varnish/{cache,}chown -R varnish.varnish /data/varnish/tar xzf varnish-${VER}.tar.gz ;cd varnish-${VER} ;/bin/sh autogen.sh  ;./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings &&make &&make install
elseecho "This Varnish is exists,Please exit..."sleep 1exit 0
fi
}function config()
{if  [ ! -d /usr/local/varnish/ -o -f /etc/init.d/varnishd ];thenecho "You can't config varnish ,Please ensure you varnish dir is or not exist..."exit 0elseecho "Varnish Already Success Install ,Please Config varnish ......"fimv /usr/local/varnish/etc/varnish/default.vcl /usr/local/varnish/etc/varnish/default.vcl.bakcat >>/usr/local/varnish/etc/varnish/default.vcl <<EOFbackend server_1
{
.host ="192.168.149.128";
.port = "8080";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 8;
.threshold = 5;
}
}
backend server_2
{
.host ="192.168.149.129";
.port = "8080";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 8;
.threshold = 5;
}
}
director rsver random {
{
.backend = server_1;
.weight = 6;
}
{
.backend = server_2;
.weight = 6;
}
}
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv
{if (req.http.host ~"^(.*).tdt.com"){     set req.backend =rsver; }  else{     error 200 "Nocahce for this domain"; }           if (req.request =="PURGE"){        if (!client.ip ~purge){           error 405"Not allowed.";        }else{return (pipe);}
}
if(req.http.x-forwarded-for)
{
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For "," client.ip;
}
else
{
set req.http.X-Forwarded-For =client.ip;
}
if (req.request !="GET" && req.request != "HEAD")
{
return (pipe);
}
if (req.http.Expect)
{
return (pipe);
}
if (req.http.Authenticate|| req.http.Cookie)
{
return (pass);
}
if (req.http.Cache-Control~ "no-cache")
{
return (pass);
}
if(req.url ~"\.jsp" || req.url ~ "\.php" )
{
return (pass);
}
else
{
return (lookup);
}
}sub vcl_pipe
{
return (pipe);
}sub vcl_pass
{
return (pass);
}sub vcl_hash
{
set req.hash += req.url;
if (req.http.host)
{
set req.hash +=req.http.host;
}
else
{
set req.hash +=server.ip;
}return (hash);
}sub vcl_hit
{
if (req.request =="PURGE")
{
set obj.ttl = 0s;
error 200"Purged.";
}
if (!obj.cacheable)
{
return (pass);
}
return (deliver);
}sub vcl_miss
{
if (req.request =="PURGE")
{
error 404 "Not incache.";
}
if (req.http.user-agent ~"spider")
{
error 503 "Notpresently in cache";
}return (fetch);
}
sub vcl_fetch
{
if (req.request =="GET" && req.url ~ "\.(txt|js)$")
{
set beresp.ttl = 3600s;
}
else
{
set beresp.ttl = 30d;
}
if (!beresp.cacheable)
{
return (pass);
}
if (beresp.http.Set-Cookie)
{
return (pass);
}
return (deliver);
}
sub vcl_deliver {if (obj.hits > 0) {set resp.http.X-Cache= "HIT FROM TDTWS Cache Center";} else {set resp.http.X-Cache= "MISS FROM TDTWS Cache Center";}
return (deliver);
}EOFif [ $? == 0 ];thenecho '----------------------------------'sleep 2echo "This Varinsh Config Success !!!"
elseecho "This Varinsh Config Failed,Please Check Conf!"
fi}function start()
{
if  [ ! -d /usr/local/varnish -o -f /etc/init.d/varnishd ];thenecho "You can't config varnish ,Please ensure you varnish dir is or not exist..."exit 0elsecount=`ps -ef |grep varnishd|grep -v grep |wc -l`if [ $count -eq 0 ];thenecho "Varnish Already Success Install ,Now start varnish...."cat <<EOF------------------------------------------------------Start Varnish to listen 0.0.0.0:80.The Varnish load balancer server1(192.168.149.128 8080).The Varnish load balancer server1(192.168.149.129 8080).The Varnish Mgr address to listen 0.0.0.0:8001.The Varnish Cache DIR /data/varnish/cache .
EOF/usr/local/varnish/sbin/varnishd -n /data/varnish/cache -f /usr/local/varnish/etc/varnish/default.vcl -a 0.0.0.0:80 -s file,/data/varnish/varnish_cache.data,16G  -p user=varnish -p group=varnish -p default_ttl=14400 -p thread_pool_max=8000 -p send_timeout=20 -w 5,51200,30 -T 0.0.0.0:8001  -P /usr/local/varnish/var/varnish.pidif [ $? == 0 ];thenecho "Start varnish ...OK"fielseecho "Warning,This Varnish Service is exist."fi
fi
}case $2 ininstall)install;;config)config;;start )start;;*    )echo "Usage:{ Usage $0 version install |config |start|help }";;
esac

Varnish简单测试如下图:

1.第一张图,第一次访问没有命中,去后端服务器取数据,同时在本地缓存一份:(MISS)

2.第二张图,第二次访问,缓存服务器存在,则直接从缓存中返回:(HIT)

实际线上环境中,如果用户访问我们的网站,使用Ctrl+F5刷新,我们的缓存就会失效,因为我们配置文件里面是这么配置的,匹配浏览器头信息:

Pragma    no-cache

Cache-Control    no-cache

if (req.http.Cache-Control~ "no-cache")
{
return (pass);
}

只有注释掉这段代码或者设置只允许某个特定的IP,用户通过浏览器按Crtl+F5才不会把缓存给清除。

下面是针对某个特定的IP地址允许刷新:

acl   local {"192.168.149.128"
}
sub vcl_hit {if (!obj.cacheable) {return (pass); }if (client.ip ~ local && req.http.Pragma ~ "no-cache") {set obj.ttl = 0s;return (pass);}return (deliver);
}

由于时间的原因,文章就暂时写到这里,更多深入的东西继续分享,文章引用煮酒哥的配置,非常感谢。欢迎大家一起讨论。

http://yuhongchun.blog.51cto.com/1604432/1293169

http://zyan.cc/post/313/

高性能缓存服务器Varnish架构配置相关推荐

  1. 高性能缓存服务器Varnish详解

    一.简介 Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好. Varnish 的作者Po ...

  2. 高性能缓存服务器 Nuster

    Nuster 是一个基于 HAProxy 的高性能缓存服务器.Nuster 完全兼容 HAProxy,并且利用 HAProxy 的 ACL 功能来提供非常细致的缓存规则,比如 请求地址为某某时缓存 请 ...

  3. Redis 作为缓存服务器的配置

    转自:http://www.open-open.com/lib/view/open1419670554109.html# 随着Redis的发展,越来越多的架构用它取代了memcached作为缓存服务器 ...

  4. Varnish缓存服务器原理

    http://mp.weixin.qq.com/s?__biz=MzA4Nzc4MjI4MQ==&mid=400762358&idx=1&sn=791c1edf90af4856 ...

  5. 配置本地缓存服务器(一)

    配置本地缓存服务器(一) 绝大多数系统管理员都遇到过开发部门抱怨软件包下载慢,严重影响工作效率的问题.对此问题,系统管理员也很无奈,无论接入带宽是10M.100M还是1G光纤,都不能解决这个问题.原因 ...

  6. 缓存服务器Cache Server 6.0发布

    无论是在个人的本地电脑,还是在团队的局域网专有服务器上,缓存服务器都能通过优化资源导入过程让使用Unity开发的速度变得更快.远程缓存服务器Cache Server 6.0版本现已发布,缓存服务器的质 ...

  7. 服务器后端开发系列——《实战Nginx高性能Web服务器》

    1.高性能Web服务器Nginx的配置与部署研究(1)Nginx简介及入门示例 内容:概述Nginx的背景知识和简单的入门实例. 2.高性能Web服务器Nginx的配置与部署研究(2)Nginx入门级 ...

  8. EasyDSS高性能流媒体服务器前端重构

    本文围绕着实现EasyDSS高性能流媒体服务器的前端框架来展开的,具体EasyDSS的相关信息可在:www.easydss.com 找到! EasyDSS 高性能流媒体服务器前端架构概述 EasyDS ...

  9. webpack服务器性能,高性能流媒体服务器EasyDSS前端重构(三)- webpack + vue + AdminLTE 多页面引入 element-ui...

    接上篇 本文围绕着实现EasyDSS高性能流媒体服务器的前端框架来展开的,具体EasyDSS的相关信息可在:www.easydss.com 找到! element-ui 介绍 饿了么前端开发组件框架 ...

最新文章

  1. ajax传值从前台到后台乱码,jquery ajax传值,get方式后台中文乱码
  2. Bjarne:如何对付内存泄漏?
  3. 【DP】【递归】分离与合体
  4. linux nohup 后台运行
  5. mysql执行代码段_mysql的event schedule 可以让你设置你的mysql数据库再某段时间执行你想要的动作...
  6. linux系统 远程桌面连接到服务器,Ubuntu 14.04服务器远程桌面连接
  7. python内置函数next()用来返回文件下一行_Python内置函数 next的具体使用方法
  8. 概率图模型笔记(二) 隐马尔科夫模型(Hidden Markov Model)
  9. Web2.0创业者面临艰难选择:出售还是发展
  10. mac使用jeb记录
  11. modelandview 跳转问题_ModelAndView 跳转的使用
  12. react-native 修改app应用名称
  13. 《计算机组成原理》作业,《计算机组成原理》作业一解答.doc
  14. 念荆轲[原创诗一首]
  15. LaTeX 算法代码排版 --latex2e范例总结
  16. 世界就是一个班(转自大鸟BLOG)
  17. 幼儿园小游戏:小兔种萝卜
  18. 亚太数字经济发展联盟 助力民营企业进入数字经济新时代
  19. You need Perl 5 安装openssl时报错,提示需要安装perl 5
  20. php实现分时线图,史上最全分时图买卖点图解(转发收藏)!

热门文章

  1. 6-3 十进制转二进制(顺序栈设计和应用) (8 分)
  2. 4004-基于邻接矩阵的新顶点的增加(C++,附思路)
  3. 邻接矩阵存储图的深度优先遍历
  4. spring boot配置虚拟路径(替代docBase配置)访问本地图片
  5. live linux 密码,Linux_如何使你的Linux系统省略输入用户名密码,在做Livecd的时候总是要输入用 - phpStudy...
  6. java 1.8.0 安全_RedHat安全更新修复OpenJDK1.8.0版本漏洞
  7. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
  8. UVA12633 Super Rooks on Chessboard
  9. Shell脚本之grep
  10. App流量测试--使用安卓自身提供的TCP收发长度统计功能