[文章作者:张宴 本文版本:v1.2 最后修改:2008.01.02 转载请注明出处:http://blog.zyan.cc]

  我曾经写过一篇文章──《初步试用Squid的替代产品──Varnish Cache网站加速器》,但当时仅仅是用着玩,没做深入研究。

  今天写的这篇关于Varnish的文章,已经是一篇可以完全替代Squid做网站缓存加速器的详细解决方案了。网上关于Varnish的资料很少,中文资料更是微乎其微,希望本文能够吸引更多的人研究、使用Varnish。

  在我看来,使用Varnish代替Squid的理由有三点:
  1、Varnish采用了“Visual Page Cache”技术,在内存的利用上,Varnish比Squid具有优势,它避免了Squid频繁在内存、磁盘中交换文件,性能要比Squid高。
  2、Varnish的稳定性还不错,我管理的一台图片服务器运行Varnish已经有一个月,没有发生过故障,而进行相同工作的Squid服务器就倒过几次。
  3、通过Varnish管理端口,可以使用正则表达式快速、批量地清除部分缓存,这一点是Squid不能具备的。

  点击在新窗口中浏览此图片


  下面来安装Varnish网站缓存加速器(Linux系统):
  1、创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache


  2、创建Varnish日志目录(/var/logs/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs


  3、编译安装varnish:
wget http://blog.zyan.cc/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install


  4、创建Varnish配置文件:
vi /usr/local/varnish/vcl.conf

  输入以下内容:
引用
backend myblogserver {
       set backend.host = "192.168.0.5";
       set backend.port = "80";
}

acl purge {
       "localhost";
       "127.0.0.1";
       "192.168.1.0"/24;
}

sub vcl_recv {
       if (req.request == "PURGE") {
               if (!client.ip ~ purge) {
                       error 405 "Not allowed.";
               }
               lookup;
       }

       if (req.http.host ~ "^blog.zyan.cc") {
               set req.backend = myblogserver;
               if (req.request != "GET" && req.request != "HEAD") {
                       pipe;
               }
               else {
                       lookup;
               }
       }
       else {
               error 404 "Zhang Yan Cache Server";
               lookup;
       }
}

sub vcl_hit {
       if (req.request == "PURGE") {
               set obj.ttl = 0s;
               error 200 "Purged.";
       }
}

sub vcl_miss {
       if (req.request == "PURGE") {
               error 404 "Not in cache.";
       }
}

sub vcl_fetch {
       if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
               set obj.ttl = 3600s;
       }
       else {
               set obj.ttl = 30d;
       }
}

  这里,我对这段配置文件解释一下:
  (1)、Varnish通过反向代理请求后端IP为192.168.0.5,端口为80的web服务器;
  (2)、Varnish允许localhost、127.0.0.1、192.168.0.***三个来源IP通过PURGE方法清除缓存;
  (3)、Varnish对域名为blog.zyan.cc的请求进行处理,非blog.zyan.cc域名的请求则返回“Zhang Yan Cache Server”;
  (4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
  (5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

  5、启动Varnish
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on


  6、启动varnishncsa用来将Varnish访问日志写入日志文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &


  7、配置开机自动启动Varnish
vi /etc/rc.local

  在末尾增加以下内容:
引用
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &


  8、优化Linux内核参数
vi /etc/sysctl.conf

  在末尾增加以下内容:
引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000



  再看看如何管理Varnish:
  1、查看Varnish服务器连接数与命中率:
/usr/local/varnish/bin/varnishstat

  点击在新窗口中浏览此图片

  2、通过Varnish管理端口进行管理:
  用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help

引用
Available commands:
ping [timestamp]
status
start
stop
stats
vcl.load
vcl.inline
vcl.use
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool


  3、通过Varnish管理端口,使用正则表达式批量清除缓存:
  (1)、例:清除类似http://blog.zyan.cc/a/zhangyan.html的URL地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/

  (2)、例:清除类似http://blog.zyan.cc/tech的URL地址:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$

  (3)、例:清除所有缓存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$


  4、一个清除Squid缓存的PHP函数(清除Varnish缓存同样可以使用该函数,无需作任何修改,十分方便):


  附1:Varnish官方网站:http://www.varnish-cache.org/

  附2:2007年12月10日,我写了一个每天0点运行,按天切割Varnish日志,生成一个压缩文件,同时删除上个月旧日志的脚本(/var/logs/cutlog.sh):
  /var/logs/cutlog.sh文件内容如下:
引用
#!/bin/sh
# This file run at 00:00
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz

  设置在每天00:00定时执行:
  
/usr/bin/crontab -e
  或者  
vi /var/spool/cron/root
  输入以下内容:
引用
0 0 * * * /bin/sh /var/logs/cutlog.sh



Tags: , , ,



技术大类 » Cache与存储 | 评论(8351) | 引用(0) | 阅读(691049)
日记本
2011-6-28 12:09
张老师,你的配置 -w 30000,51200,10  这句话是什么意思呢?
xcxc Homepage
2011-9-27 18:01
fds376bvcd Email Homepage
2011-9-27 19:39
本人拍拍小店正在热卖:"韩版 个性 气质 花苞领 单排扣 大翻领 长袖 羊毛 尼大衣 371",discount christian louboutin sandals
强烈推荐大家去看看>>
价格:118.80元
运费支付:平邮:10.00元 快递:10.00元
付款方式:财付通
新旧程度:全新
所 在 地:海外 海外
本人小店还有更多精品,Louis Vuitton Luggage,欢迎大家来逛逛>>
fds480bvcd Email Homepage
2011-9-27 19:40
相爱的时候.女人会一次次地提出:我们分手吧!
男人只是本能的愤怒,他会猜疑她是不是因为另有新欢而背叛了他,
他会气恼女人的绝情而大声呵斥她,在女人真正转身地那一刻,
男人除了悲愤地看着她的背影离去而没有一句挽留!    
女人一路上一直期待男人会跑上来,拉着她的手,挽留她.
说声:宝贝,我爱你!别走!等到泪已尽,仍然听不到任何声响,爱情就这样夭折了!  
男人怎懂女人?说分手只是为了被挽留!  
每一次说分手.女人都会很害怕,christian louboutin pumps,怕你们会真的离去.每一次说分手.
女人都很期待,期待你们的挽留,让她知道你在乎他,你舍不得她走.
每一次说分手.女人都很无奈.你的一些微妙变化让她不再肯定你是否还那样爱她.
所以她拿放弃做赌注.如果输了.只是你真的不够爱她!每一次当分手成了事实.
女人会伤心欲绝,男人为什么不懂女人的心思??  
女人说分手,只是真的爱你!只是太在乎你!只是你的一些微妙变化让女人恐慌!
让女人心不安!只是女人想弄明白你是否还爱着她?  
女人以为,爱情的迷茫不肯定会让自己有足够的勇气,做好准备等待男人最糟糕的答案,
女人以为爱情就象一个开关,啪地一声打开,啪地一声关闭,
女人以为及时拨掉电源就可以幸免于毁灭,女人以为分手可以解决所有的困惑、痛苦、忧郁,
女人以为缓慢的生长可以愈合此处的断裂,女人以为她说分手你会挽留她!  
然而这都是只女人的一相情愿,多少此时的男人是默默看着自己心爱的女人离去而没有挽留?
女人的心凉了,为什么男人不懂女人的心思?  
女人只有独自在黑夜中哼着悲曲,用泪水把心中的苦涩一遍遍洗刷……  
你不够爱我  
也许某一天  
我想起你也是一件很远很远的事  
那一天,在未来的未来  
再远处,是衰老,更远处,是死亡,Christian Louboutin Boots!  
近处是分手,其实是真的爱你,cheap ugg boots,太害怕失去你!只是你永远不懂!还是不懂!
louis vuitton uk Email Homepage
2011-11-23 08:53
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
rjmcy
2011-12-12 10:36
varnishd默认会缓存后端返回的404页面,在varnishd 3版本只上,如何不让varnishd缓存后端返回的大于400状态码,谢谢
gyqgtgt
2011-12-14 16:17
hi, 问下,varnish 对 etag 这个支持怎么样啊
zaq1234123 Email Homepage
2011-12-14 17:01
网帖发出后,范洁平步青云的仕途引发众多网友质疑。11月30日下午,湖北江陵县委组织部对此回应,“范洁毕业时以选调生的身份入职,后面通过省里的‘年轻干部成长工程’得以进入县里”,并称“范洁职务的升迁都是公开透明地进行、经得起推敲和质疑”。
    网友“琴瑟无音”的网帖发布后,引来众多网友围观,其平步青云的仕途成为网友质疑的焦点。
    网友“zhaxirandan”则认为,“现在乡镇一级都升得较快,26岁担任县委常委虽属年轻,但并无不妥。”
    对于网友质疑范洁频繁升迁一事,这位工作人员表示,范洁升迁“与后台和背景之类的没有关系”。据该工作人员介绍,范洁本科毕业后,通过参加选调生考试进入江陵县滩桥镇工作,“后来得益于湖北省‘年轻干部成长工程’,进入县里面工作”。
    网友“lyt1982”回帖称,“毕业当镇团委书记,次年就升任副镇长,不会是女超人吧?可惜的是四年升迁四次,做出了啥不同凡响的成绩没。”网友“jidili”则质疑,少女文胸品牌,“从其简历来看,科员升副科,副科升科长都不足一年,这样也可以?”
江陵县26岁县委常委引发网友热议。 网络截图
    网帖内容显示,范洁自2008年6月从华中农业大学毕业后,于当年7月担任滩桥镇党政综合办公室副主任、团委书记,次年4月升任滩桥镇副镇长、党委委员,2010年3月升迁至滩桥镇党委副书记、镇长。今年9月,范洁再次升迁,担任滩桥镇党委书记、人大主席; 10月,范洁升任江陵县委常委,滩桥镇党委书记、人大主席;11月至今,【SMM】SMM2011中文官网全程直播合作,范洁担任江陵县委常委、统战部部长,滩桥镇党委书记、人大主席。
回应:升迁经得起推敲和质疑
爆料:26岁县委常委四年升迁四次
    11月30日下午,网友“琴瑟无音”在华声论坛发帖,称湖北江陵县委常委、统战部部长范洁是“史上最牛县委常委”。据网帖爆料,范洁出生于1985年,自2008年本科毕业后,不到四年时间连升四次,现任江陵县委常委、统战部部长,滩桥镇党委书记、人大主席。
zaq1234123 Email Homepage
2011-12-14 17:03
回应:升迁经得起推敲和质疑
    爆料:26岁县委常委四年升迁四次



    网帖发出后,南方人才网 湖北江陵县26岁女常委本科毕业4年升迁4次(,范洁平步青云的仕途引发众多网友质疑。11月30日下午,湖北江陵县委组织部对此回应,“范洁毕业时以选调生的身份入职,后面通过省里的‘年轻干部成长工程’得以进入县里”,并称“范洁职务的升迁都是公开透明地进行、经得起推敲和质疑”。



    网友“琴瑟无音”的网帖发布后,引来众多网友围观,其平步青云的仕途成为网友质疑的焦点。



江陵县26岁县委常委引发网友热议。 网络截图
    网友“zhaxirandan”则认为,“现在乡镇一级都升得较快,26岁担任县委常委虽属年轻,但并无不妥。”



    “湖北江陵县诞生了史上最牛县委常委,副处级干部哪,县委常委哪,只有26岁哇!”11月30日,有网友在华声论坛发帖爆料,51.com,出生于1985年的范洁自2008年大学本科毕业后,直接担任江陵县滩桥镇党政综合办公室副主任、团委书记,并在不到四年的时间里,升迁四次,现任江陵县委常委、统战部部长,滩桥镇党委书记、人大主席。


    网帖内容显示,范洁自2008年6月从华中农业大学毕业后,于当年7月担任滩桥镇党政综合办公室副主任、团委书记,次年4月升任滩桥镇副镇长、党委委员,2010年3月升迁至滩桥镇党委副书记、镇长。今年9月,范洁再次升迁,担任滩桥镇党委书记、人大主席;10月,范洁升任江陵县委常委,滩桥镇党委书记、人大主席;11月至今,范洁担任江陵县委常委、统战部部长,滩桥镇党委书记、人大主席。
    网友“lyt1982”回帖称,“毕业当镇团委书记,次年就升任副镇长,不会是女超人吧?可惜的是四年升迁四次,做出了啥不同凡响的成绩没。”网友“jidili”则质疑,“从其简历来看,科员升副科,副科升科长都不足一年,这样也可以?”

    对于网友质疑范洁频繁升迁一事,这位工作人员表示,范洁升迁“与后台和背景之类的没有关系”。据该工作人员介绍,范洁本科毕业后,通过参加选调生考试进入江陵县滩桥镇工作,“后来得益于湖北省‘年轻干部成长工程’,进入县里面工作”。
    11月30日下午,网友“琴瑟无音”在华声论坛发帖,称湖北江陵县委常委、统战部部长范洁是“史上最牛县委常委”。据网帖爆料,范洁出生于1985年,自2008年本科毕业后,不到四年时间连升四次,现任江陵县委常委、统战部部长,滩桥镇党委书记、人大主席。
zaq1234123 Email Homepage
2011-12-14 17:17
,湖北26岁女县委常委四年升迁四次 回应称经得起质疑 - 地方联播 - 新华网
登录失败:用户昵称或密码输入错误请重试

登录失败:用户昵称或密码输入错误请重试
只要是民选的,吴彦祖,26岁当国家主席我都没意见
coach factory outlet Email Homepage
2012-5-17 11:16
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:16
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:16
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:16
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
xujie777 Email
2012-5-18 16:34
We aim to make all our customers satisfy with our products. You will find a variety of Men's fashion louis vuitton uk, fashion Women's cheap Louis Vuitton bags in our store at affordable price.Thinking of interesting ways to cost a milestone birthday? louis vuitton online shop had one of the most distinctive distinctive celebrations.bakery along with living room operated by means of about three moment louis vuitton online Most effective Pastry Chef’s of the year Rammy Nominee Chef’s.
xujie777 Email
2012-5-18 16:34
The choices are likely to be basically countless seeing that louis vuitton outlet occurs with the help of completely new and also incredible concepts once in a while.Louis vuitton Wholesale Monogram Canvas HandbagsLouis Vuitton Collection Beach Handbags louis vuitton bags outlet Damier Canvas HandbagsLouis vuitton Mahina HandbagsLouis Vuitton Monogram Mini Lin HandbagsLouis Vuitton Monogram Multicolore HandbagsLouis vuitton Monogram Vernis HandbagsLouis Vuitton Wholesale Epi Leather HandbagsLouis Vuitton For Men HandbagsLouis Vuitton Damier Canvas WalletsLouis Vuitton Epi Leather WalletsLouis Vuitton Monogram Canvas WalletsLouis Vuitton Monogram Vernis WalleLouis Vuitton ShoesLouis Vuitton Men wallets.As the Authentic Louis Vuitton are so high-priced, so came the louis vuitton handbags outlet.
xujie777 Email
2012-5-18 16:34
coach outlet has become a popular shopping experience for consumers around the world, and a desirable distribution channel for manufacturer's and retailers.Getting your hands on coach outlet store online can be a hefty investment of hundreds of dollars.But do not despair,the Coach Outlet store could be the answer to your prayers.coach outlet online is your smart choice when you want to get the discount Coach accessory. You can find the exact Coach Bags and other accessory you want at a low price that's right for you. My dear friends, let the coach outlet online pave your way into the world of high fashion with their ultimate fashion factory.
非鸡类 Homepage
2012-5-19 16:41
xujie123
2012-5-21 11:17
Our online store offers you discounted Designer louis vuitton replica wallet at present. You could find them in desirable quality and price. If you don't mind high class louis vuitton uk, have a good time here.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.Have you ever dreamed of being as charming as Madonna? Have you ever thought of becoming an envy of all your friends? If so, come to louis vuitton outlet.<br/>
xujie555
2012-5-21 13:37
coach factory outlet uses graceful accessories to match the classical logo of coach, which is the best combination of coach. The handmade coach products?can make you more charming and graceful.coach factory online provides people many coach goods. If you wish to snatch the coach handbag, then this best method is made for that you like for coach discount.coach factory outlet online is a fashion brand to ensure its quality. With designer coach shoulder bags, you will always attract people's attention. The bags will emphasize your personal style and taste.<br/>
分页: 4/418 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]