varnish安装配置

wget http://sourceforge.net/projects/varnish/files/varnish/2.1.3/varnish-2.1.3.tar.gz/download
tar zxvf varnish-2.1.3.tar.gz
cd varnish-2.1.3

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure -prefix=/usr/local/varnish
make
make install



启动Varnish:
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128M -T 127.0.0.1:2500 -a 0.0.0.0:80

查看状态:
/usr/local/varnish/bin/varnishstat

查看Referer:
/usr/local/varnish/bin/varnishtop -i rxheader -I Referer

查看访问路径:
/usr/local/varnish/bin/varnishtop -i rxurl


配置文件说明:
(1)、Varnish通过反向代理请求后端IP为74.82.170.235,端口为80的web服务器;
(2)、Varnish允许localhost、127.0.0.1、74.82.170.235三个来源IP通过PURGE方法清除缓存;
(3)、Varnish对域名为www.linuxidc.com的请求进行处理,非www.linuxidc.com域名的请求则返回“Cai Yuan Zhi Cache Server”;

(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。
# 创建缓存目录和日志文件目录
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache
mkdir -p /var/log/varnish
chmod +w /var/log/varnish
chown -R www:www /var/log/varnish

# 启动Varnish
# /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

参数说明:
-n 缓存写入路径
-f 指定配置文件启动
-a 监听本机的网卡的80端口
-T 指定本机的varnish管理端口
-s file 指定varnish缓存文件的位置以及大小
-w 指处理的最小请求数、最大请求数、超时时间
-g 组名
-u 用户名
-p client_http11=on 支持http1.1协议
-P 指定其进程码文件的位置

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

# 通过 Varnish 管理端口�M行管理
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help

# 清除具体URL地址
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$
# 清除所有缓存
# /usr/local/varnish-2.1/bin/varnishadm -T 127.0.0.1:3500 url.purge *$

# 通过varnishstat监控varnish状态
/usr/local/varnish/bin/varnishstat �Cn var/vcache

///////////////////////////////////////

#如果使用虚拟主机,请参照下面代码 
            backend www { 
             set backend.host = "www.example.com"; 
             set backend.port = "80"; 
         }

         backend images { 
             set backend.host = "images.example.com"; 
             set backend.port = "80"; 
         }

         sub vcl_recv { 
             if (req.http.host ~ "^(www.)?example.com$") { 
                 set req.backend = www; 
             } elsif (req.http.host ~ "^images.example.com") { 
                 set req.backend = images; 
             } else { 
                 error 404 "Unknown virtual host"; 
             } 
         }  
    #关于cache存在时间设置 
             sub vcl_fetch { 
             if (obj.ttl < 120s) { 
                 set obj.ttl = 120s; 
             } 
         }



########################################################################################################################################
4.通过Varnish管理端口进行管理:
列出所有管理命令

# /usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 help

使用正则清理Varnish缓存
例1. 清理url中带有 /00/的地址: /00/

# /usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 url.purge /00/

例2.清除.jpg图片缓存: jpg$

# /usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 url.purge jpg$

例3.清除所有: *

# /usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 url.purge *






顺带提一下Varnish不重启进程让修改后的配置文件立刻生效的方法

1. telnet登陆到Varnish的管理端口

2. vcl.load <configname> <configpath>

configname可以自己设定,想叫啥都行。configpath就是配置文件的完整路径,比如/usr/local/varnish/etc/varnish/default.vcl

3. vcl.use <configname>

第二步和第三步如果成功会返回代码200,全部操作完成后退出即可。

关于管理台的运用其实很简单,telnet登陆上以后执行help就一目了然了。


vcl.load neirong /usr/local/varnish/etc/varnish/neirong.vcl

vcl.use neirong



vcl.load neirong /usr/local/varnish/etc/varnish/image.vcl

你可能感兴趣的:(职场,缓存,varnish,休闲,Varnish管理端口)