这篇文章主要为大家详细介绍了Linux下Varnish缓存配置优化的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。代码如下:
backend linuxidc01 {
.host = "192.168.1.142";
.port = "7070";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 10;
.threshold = 8;
}
}
backend linuxidc02 {
.host = "192.168.1.141";
.port = "7070";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 10;
.threshold = 8;
}
}
代码如下:
director linuxidc15474 random
{ .retries = 6;
{ .backend = linuxidc02;
.weight = 2;
}
{ .backend = linuxidc01;
.weight = 2;
}
}
代码如下:
acl local {
"localhost";
"127.0.0.1";
}
代码如下:
sub vcl_recv
{
if (req.http.host ~ "^linuxidc15474.vicp.net") #匹配域名跳转后台服务器
{ set req.backend = linuxidc15474; }
else { error 404 "Unknown HostName!"; }
if (req.request == "PURGE") #不允许非访问控制列表内的IP清除varnish缓存
{ if (!client.ip ~ local)
{
error 405 "Not Allowed.";
return (lookup);
}
}
#清除url中有jpg等文件的cookie
if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$")
{
unset req.http.cookie;
}
#判断req.http.X-Forwarded-For 如果前端有多重反向代理,这样可以获取客户端IP地址。
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; }
##varnish实现图片的防盗链
# if (req.http.referer ~ "http://.*)
# {
# if ( !(req.http.referer ~ "http://.*vicp\.net" ||
# req.http.referer ~ "http://.*linuxidc15474\.net" ) )
# {
# set req.http.host = "linuxidc15474.vicp.net";
# set req.url = "/referer.jpg";
# }
# return(lookup);
# }
# else {return(pass);}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE")
{ return (pipe); }
#对非GET|HEAD请求的直接转发给后端服务器
if (req.request != "GET" && req.request != "HEAD")
{ return (pass); }
##对GET请求,且url里以.php和.php?结尾的,直接转发给后端服务器
if (req.request == "GET" && req.url ~ "\.(php)($|\?)")
{ return (pass); }
##对请求中有验证及cookie,直接转发给后端服务器
if (req.http.Authorization || req.http.Cookie)
{ return (pass);}
{
##除以上的访问请求,从缓存中查找
return (lookup);
}
##指定的font目录不进行缓存
if (req.url ~ "^/fonts/")
{ return (pass); }
}
sub vcl_pipe
{ return (pipe); }
##进入pass模式,请求被送往后端,后端返回数据给客户端,但不进入缓存处理
sub vcl_pass
{ return (pass); }
sub vcl_hash
{
set req.hash += req.url;
if (req.http.host)
{ set req.hash += req.http.host; }
else { set req.hash += server.ip; }
return (hash);
}
##在lookup后如果在cache中找到请求的缓存,一般以下面几个关键词结束
sub vcl_hit
{
if (!obj.cacheable)
{ return (pass); }
return (deliver);
}
##lookup后没有找到缓存时调用,以下面几个关键词结束,及调用fetch参数重新测试是否加入缓存
sub vcl_miss
{ return (fetch); }
#让varnish服务器缓存的类型,从后端取得数据后调用
sub vcl_fetch
{ if (!beresp.cacheable)
{ return (pass); }
if (beresp.http.Set-Cookie)
{ return (pass); }
##WEB服务器指明不缓存的内容,varnish服务器不缓存
if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" || beresp.http.Cache-Control ~ "private")
{ return (pass); }
##对访问中get有包含jpg,png等格式的文件进行缓存,缓存时间为7天,s为秒
if (req.request == "GET" && req.url ~ "\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$")
{ set beresp.ttl = 7d; }
##对访问get中包含htm等静态页面,缓存300秒
if (req.request == "GET" && req.url ~ "\/[0-9]\.htm$")
{ set beresp.ttl = 300s; }
return (deliver);
}
####添加在页面head头信息中查看缓存命中情况########
sub vcl_deliver
{
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0)
{ set resp.http.X-Cache = "HIT cqtel-bbs"; }
else { set resp.http.X-Cache = "MISS cqtel-bbs"; }
}
代码如下:
<?php
/* 四海网 www.q1010.com */
function purge($ip, $url)
{
$errstr = '';
$errno = '';
$fp = fsockopen ($ip, 80, $errno, $errstr, 2);
if (!$fp)
{
return false;
}
else
{
$out = "PURGE $url HTTP/1.1\r\n";
$out .= "Host:blog.s135.com\r\n";
$out .= "Connection: close\r\n\r\n";
fputs ($fp, $out);
$out = fgets($fp , 4096);
fclose ($fp);
return true;
}
}
purge("192.168.0.4", "/index.php");
?>
本文来自:http://www.q1010.com/173/15268-0.html
注:关于Linux下Varnish缓存配置优化的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。