无限星辰工作室-客户无限互联网动力之源
标题:
varnish负载均衡及故障自动切换(转载)
[打印本页]
作者:
crx349
时间:
2013-9-15 22:12
标题:
varnish负载均衡及故障自动切换(转载)
环境:有四台WEB,
WEB1-3为提供
www.test.com
的后端服务器
WEB1-2为负载均衡根据服务器情况设置权重
WEB3为健康状态检测后的备份机 意思就是如果访问WEB1-2其中一台机器出现问题的问题那么转向WEB3服务器再访问
WEB4为theme.abc.com的后端服务器
backend web1 {
.host = "192.168.0.1";
.port = "80";
}
backend web2 {
.host = "192.168.0.2";
.port = "80";
}
backend web3 {
.host = "192.168.0.3";
.port = "80";
}
backend web4 {
.host = "192.168.0.4";
.port = "80";
}
#负载轮询机制
director default random {
{
.backend = web1;
.weight = 200; }
{
.backend = web2;
.weight = 300; }
}
#允许清理缓存控制
acl purge {
"localhost";
"127.0.0.1";
"192.168.0.0"/26;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
elseif(req.url ~ "\.(php|cgi)($|\?)") {
pass;
}
else {
lookup;
}
}
#设置健康状态检测机制,这里实际是使用了一个备份功能
if((req.http.host ~"^(www.|bbs.)?test.com")&&(req.restarts == 0)) {
set req.backend = default;
} elseif(req.restarts == 1) {
set req.backend = web3;
}
#另外一个域名的配置
if(req.http.host ~"^(theme.|special.)?abc.com") {
set req.backend = web4;
}
#定义一些缓存的内容 PHP 不缓存
if (req.request != "GET" && req.request != "HEAD")
{
pipe;
}
elseif (req.url ~ "\.(php|cgi)($|\?)")
{
pass;
}
elseif (req.http.Authenticate || req.http.Authorization) {
pass;
}
lookup;
}
sub vcl_hit
{
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
if (!obj.cacheable)
{
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;
}
}
hash;
}
sub vcl_fetch
{
#轮询机制的一部分,定义什么时候就restart
if ((obj.status == 500 || obj.status == 501 || obj.status == 502 || obj.status == 503) && req.restarts < 2) {
restart;
}
if (!obj.cacheable)
{
pass;
}
#决定哪些头不缓存
if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
pass;
}
if (obj.http.Set-Cookie)
{
pass;
}
#定义不同内容的缓存时间
if (req.request == "GET" && req.url ~ "\.(txt|js|css|html|htm)$") {
set obj.ttl = 600s;
}
else {
set obj.ttl = 10d;
}
复制代码
欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://xmspace.net/)
Powered by Discuz! X3.4