Centos 5.6
安装varnish
环境
varnish 192.168.58.166
nginx1 192.168.58.130
nginx1 192.168.58.168
linux
同步北京时间
ntpdate stdtime.sinica.edu.tw
创建运行用户和目录
useradd -s /sbin/nologin varnish
mkdir /data
mkdir /data/varnish
mkdir /data/varnish/cache
mkdir /data/varnish/log
chown -R varnish:varnish /data/varnish/cache/ /data/varnish/log/
安装
pcre
yum -y install pcre*
安装
varnish
wget http://pkgs.fedoraproject.org/repo/pkgs/varnish/varnish-2.1.2.tar.gz/8b0d80e47acf4946671c381af55518b9/varnish-2.1.2.tar.gz
tar zxvf varnish-2.1.2.tar.gz -C /usr/src/
cd /usr/src/varnish-2.1.2/
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings
make && make install
新建配置文件
cd /usr/local/varnish/etc
vi vcl.conf
backend webserver1 {
.host = "192.168.58.130";
.port = "80";
}
backend webserver2 {
.host = "192.168.58.168";
.port = "80";
}
#backend webserver3 {
# .host = "192.168.12.14";
# .port = "80";
#}
#backend webserver4 {
# .host = "192.168.12.15";
# .port = "80";
#}
#
定义一个名为
webserver
的
director
,也就是由
webserver1
和
webserver2
两台后端服务器随机分担
#
请求。“
.weight
”用来指定两台后端服务器的权值。权值高的处理请求的几率就高些
director webserver random {
{.backend = webserver1; .weight = 5; }
{.backend = webserver2; .weight = 8; }
}
#
这里设定清理缓存的规则,
Varnish
允许
localhost
、
127.0.0.1
和
192.168.58.***
三个来源
IP
通过
# PURGE
方法清除缓存
acl purge {
"localhost";
"127.0.0.1";
"192.168.58.0"/26;
}
sub vcl_recv {
#
这里设定,当发送
PURGE
请求的客户端不是在
acl
中设定的地址时,将返回
405
状态代码,提示
#
“
Not allowed
”。当请求的
URL
是以
.php
和
.cgi
结尾时,则交给后端服务器去处理
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
elseif(req.url ~ "\.(php|cgi)($|\?)") {
return (pass);
}
else {
return (lookup);
}
}
#
下面设定域名访问策略,其实也是设定对后端主机健康状态检测的一个机制。如果访问
www.ixdba.net
#
或者
bbs.ixdba.net
,并且请求重启次数为
0
,则将请求交给
webserver
来处理。如果请求重启次数
#
为
1
,则将请求交给
webserver1
处理。如果访问
img.ixdba.net
或者
images.ixdba.net
,则将
#
请求交给
webserver2
来处理
# if((req.http.host ~"^(www.|bbs.)?ixdba.net")&&(req.restarts == 0)) {
# set req.backend = webserver;
# } elseif(req.restarts == 1) {
# set req.backend = webserver1;
# }
# if(req.http.host ~"^(img.|images.)?ixdba.net") {
# set req.backend = webserver2;
# }
#
上面注释部分是基于域名的访问策略,有域名的话,可以采用上面的。因为没有域名,所以下面是去除了域名的部分。
if (req.restarts == 0) {
set req.backend = webserver;
} else if(req.restarts == 1) {
set req.backend = webserver1;
} else if(req.restarts == 2) {
set req.backend = webserver2;
}
#
下面定义缓存的策略。当请求以
.cgi
和
.php
结尾及带有
?
的
URL
时,不进行缓存,直接从后端服务器
#
读取内容。其他请求都进入
lookup
模式,也就是进入
cache
中通过
hash
表寻找被请求的数据
if (req.request != "GET" && req.request != "HEAD")
{
return (pipe);
}
elseif (req.url ~ "\.(cgi|php)($|\?)")
{
return (pass);
}
elseif (req.http.Authenticate || req.http.Authorization) {
return (pass);
}
return (lookup);
}
#
如果请求的类型是
PURGE
方法,
Varnishd
会将此请求的缓存周期设置为
0
,也就是使这个
URL
的缓存失效,
#
从而达到刷新
Varnish
缓存的目的
sub vcl_hit
{
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
if (!obj.cacheable)
{
return (pass);
}
if (obj.http.Vary)
{
unset obj.http.Vary;
}
}
sub vcl_miss
{
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}
#
定义
hash
的值,并且处理压缩内容
sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
if ( req.http.Accept-Encoding ){
if (req.url ~ "\.(jpg|jpeg|png|gif|rar|zip|gz|tgz|bz2|tbz|mp3|ogg|swf|exe|flv|avi|rmvb|rm|mpg|mpeg|pdf)$") {
} else {
set req.hash += req.http.Accept-Encoding;
}
}
return (hash);
}
sub vcl_fetch
{
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
#
定义在什么状态下进入
restart
模式
if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504 || beresp.status == 404)
{
return (restart);
}
#
下面定义不缓存含有哪些
HTTP
头的请求
if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" || beresp.http.Cache-Control ~ "private") {
return (pass);
}
#
定义不同内容的缓存时间
if (req.request == "GET" && req.url ~ "\.(css|js|html|htm)$") {
set beresp.ttl = 300s;
}
if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|bmp|wmf)$") {
set beresp.ttl = 3600s;
}
if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|wav|rmvb|avi|wmv)$") {
set beresp.ttl = 10d;
}
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.xx.com";
} else {
set resp.http.X-Cache = "MISS from www.xx.com";
}
return (deliver);
}
启动
varnish
/usr/local/varnish/sbin/varnishd -u varnish -g varnish -f /usr/local/varnish/etc/vcl.conf -a 192.168.58.166:80 -s file,/data/varnish/cache/varnish_cache.data,256MB -w 256,2560,10 -t 3600 -T 192.168.58.166:3000
查看端口
[root@ns1 etc]# netstat -napt | grep varnish
tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 22907/varnishd
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 22906/varnishd
在
192.168.58.130
上面修改网页内容
vi /usr/local/nginx/html/index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx1!</h1></center>
</body>
</html>
192.168.58.168
修改为
Welcome to nginx2!
重启
nginx
/usr/local/nginx/sbin/nginx -s reload
客户端访问
varnish
http://192.168.58.166/
关闭
nginx1
服务
/usr/local/nginx/sbin/nginx -s stop
等待一段时间
清楚缓存,再次访问页面,就看到
nginx2