Varnish安装搭建配置缓存服务器

官方下载地址:

wget http://www.varnish-software.com/sites/default/files/varnish-2.1.4.tar.gz


标准安装:

yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
cd varnish-2.1.4
./configure --prefix=/usr/local/varnish
make && make install


简单配置文件:cat /usr/local/varnish/etc/varnish/default.vcl

backend default {
.host = "127.0.0.1";
.port = "9999";
}
#定义后端应用端口
sub vcl_recv {
if (req.restarts == 0) {
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.url ~ "\.(png|gif|jpg|swf|css|js)$") {
return (lookup);
}
return (pass);
}
#图片、swf等静态文件设置缓存,其他类型文件pass到后端.
sub vcl_pipe {
set bereq.http.connection = "close";
return (pipe);
}

sub vcl_pass {
return (pass);
}



启动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.xtgly.com的请求进行处理,非www.xtgly.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 管理端口進行管理
# /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 –n var/vcache

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

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

backendimages{
setbackend.host="images.example.com";
setbackend.port="80";
}

subvcl_recv{
if(req.http.host~"^(www.)?example.com$"){
setreq.backend=www;
}elsif(req.http.host~"^images.example.com"){
setreq.backend=images;
}else{
error404"Unknownvirtualhost";
}
}
#关于cache存在时间设置
subvcl_fetch{
if(obj.ttl<120s){
setobj.ttl=120s;
}
}

http://bbs.chinaunix.net/viewthread.php?tid=987084

http://www.604f.com/read.php?3

http://www.xtgly.com/2010/12/09/varnish-2-1-4-%E5%9C%A8-centos-5-5-%E4%B8%8B%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85.htm

你可能感兴趣的:(应用服务器,PHP,centos,cache,配置管理)