一、varnish优点
1、数据存储,可以使用内存缓存,也可以使用磁盘大文件存储,减少小文件频繁读写
2、充分利用多核cpu,支持http/1.1协议
3、可以使用正则匹配缓存,配置灵活,思路清晰,管理端口,工具工具使用方便
二、工作原理流程
三、安装及其配置
官方地址https://www.varnish-cache.org/
tar varnish-3.0.0.tar.gz
cd varnish-3.0.0
./configure --prefix=/usr/local/varnish3.0
make && make install
配置实例及说明
vim /usr/local/varnish3.0/etc/varnish/default.vcl
#设置后端服务
backend linuxidc {
.host = "192.168.100.5";
.port = "80";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
backend 88181 {
.host = "192.168.100.6";
.port = "80";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
#定义负载均衡
director lb_test random {
{
.backend = linuxidc;
.weight = 5;
}
{
.backend = 88181;
.weight = 5;
}
}
#定义访问控制列表
acl purge {
"localhost";
"127.0.0.1";
"192.168.100.0"/24;
"192.168.0.0"/24;
}
sub vcl_recv {
#开启压缩模式,图片格式取消压缩
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {
remove req.http.Accept-Encoding;
remove req.http.Cookie;
} else if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
#根据host设置后端服务器
if (req.http.Host ~ "(?i)(|www.88181.com)") {
set req.backend = lb_test;
}else
if (req.http.Host ~ "(?i)image.Android.com"){
set req.backend = linuxidc;
}else
{
error 408 "Hostname not found";
}
#如果为purge请求,客户端ip不在访问列表中,返回405拒绝
if (req.request == "PURGE") {
if (!client.ip ~purge) {
error 405 "Not Allowed";
}
#本地缓存查找
return(lookup);
}
#如果为GET请求,url后缀为jpg,png,gif等 取出cookie
if (req.request == "GET"&&req.url ~ "(?i)\.(jpg|png|gif|swf|jpeg|ico)$") {
unset req.http.cookie;
}
#如果GET请求,url为php,则穿过cache,不缓存
if (req.request =="GET"&&req.url ~ "(?i)\.php($|\?)"){
return (pass);
}
#简单防盗链
if (req.http.referer ~ "http://.*") {
if ( !(req.http.referer ~ "http://.*linuxidc\.com"
|| req.http.referer ~ "http://.*88181\.com"
|| req.http.referer ~ "http://.*android\.com"
|| req.http.referer ~ "http://.*google\.com"
|| req.http.referer ~ "http://.*baidu\.com"
|| req.http.referer ~ "http://.*yahoo\.cn"
)) {
error 404 "Not Found!";
}
}
#获取客户端ip
# 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;
}
# }
#不是以下请求进入pipe模块
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
#不是GET 和HEAD请求不缓存
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
#
sub vcl_pipe {
return (pipe);
}
#
sub vcl_pass {
return (pass);
}
#使用url+host hash算法查找数据
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
# 如果请求为purge 将清除缓存
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged";
}
return (deliver);
}