varnish安装、配置、使用

一、安装、配置

yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig  
下载varnish软件包
cd varnish-2.1.2
./configure --prefix=/usr/local/varnish
make && make install
cd /usr/local/varnish/etc/varnish
编辑配置文件 vcl.conf
附件:
检查配置文件/usr/local/varnish/sbin/varnishd -C -f /usr/local/varnish/etc/vcl.conf
启动
/usr/local/varnish/sbin/varnishd -u varnish -g varnish -f /usr/local/varnish/etc/varnish/vcl.conf  -a 0.0.0.0:80 -s malloc,4G -T 127.0.0.1:8888
二、常用命令
启动: /usr/local/varnish/sbin/varnishd -u varnish -g varnish -f /usr/local/varnish/etc/varnish/vcl.conf  -a 0.0.0.0:80 -s malloc,4G -T 127.0.0.1:8888
检查配置文件: /usr/local/varnish/sbin/varnishd -C -f /usr/local/varnish/etc/varnish/vcl.conf
重载配置文件:
telnet 127.0.0.1 8888
vcl.load new.vcl /usr/local/varnish/etc/varnish/vcl.conf
vcl.use new.vcl
记录日志:
/usr/local/varnish/bin/varnishncsa -w /data/varnish/varnish.log &
重载缓存:
varnishreplay -a 127.0.0.1:80 -r /data/varnish/varnish.log
清除缓存
telnet 127.0.0.1 8888
help
purge.url test.png  清除test.png的缓存
清除http://izhoufeng.com/somedirectory/和目录下的所有页面。

purge req.http.host == izhoufeng.com && req.url ~ ^/somedirectory/.*$
or
purge req.url ~ ^/somedirectory/ && req.http.host == izhoufeng.com
清除所有带“Cache-Control: max-age=3600”的对象。
purge obj.http.Cache-Control ~ max-age=3600


varnish补充:

遇到问题:缓存图片后更换图片内容,无法清除缓存

需要修改配置文件

在vcl_hit段,加入如下配置

if ( req.http.Pragma ~ "no-cache" ) {
       set obj.ttl = 0s ;
       return (pass);

这段代码告诉varnish, 当请求header Pragma中包含no-cache时,清理缓存(obj.ttl=0s),并直接从后端服务器取得数据( 即return (pass) );

你可能感兴趣的:(Varnish安装配置)