我使用 yum 安装了 varnish3.03!但我遇到了问题,我的 varnish 配置如下:**
#
#backend weibo {
.host = "192.168.1.178";
.port = "8080";
.connect_timeout=20s;
.first_byte_timeout=20s;
.between_bytes_timeout=20s;
}
#backend smth {
.host = "192.168.1.115";
.port = "8080";
.connect_timeout=20s;
.first_byte_timeout=20s;
.between_bytes_timeout=20s;
}
#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.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);
}
if (req.request != "GET" && req.request != "HEAD") {
# /* We only deal with GET and HEAD by default */
return(pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return(pass);
}
if (req.http.host ~ "^(hk.)?weibo.com"){
set req.http.host = "hk.weibo.com";
set req.backend = weibo;
}
elseif (req.http.host ~ "^(www.)?newsmth.net"){
set req.http.host = "www.newsmth.net";
set req.backend = smth;
}
else {
error 404 "Unknown virtual host";
}
return(lookup);
}
##sub vcl_pipe {
return(pipe);
}
#sub vcl_pass {
return(pass);
}
#sub vcl_hash {
hash_data(req.url);
if(req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return(hash);
}
#sub vcl_hit {
if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){
set obj.ttl=0s;
return (restart);
}
return(deliver);
}
#sub vcl_miss {
return(fetch);
}
#sub vcl_fetch {
if (beresp.ttl <= 120s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 10s;
return (hit_for_pass);
}
return(deliver);
}
#sub vcl_deliver {
return(deliver);
}
#sub vcl_init {
return(ok);
}
#sub vcl_fini {
return(ok);
}
我的 Win7 的 hosts 文件添加如下内容:
192.168.1.178 www.newsmth.net
192.168.1.178 hk.weibo.com
开始上漆
varnishd -f /etc/varnish/dd.vcl -s malloc,100M -a 0.0.0.0:8000 -T 0.0.0.0:3500<br>
但是当我访问“hk.weibo.com:8000”时一切正常,并得到:
Hello,I am hk.weibo.com!
但当访问时http://www.newsmth.net:8000/
,得到:
Hello,I am hk.weibo.com! <br>
我的问题是为什么它不是“你好,我是www.newsmth.net!”?
varnish 从错误的后端获取了内容。有人知道如何修复这个问题吗?
答案1
我的猜测是,它首先会遇到以下块,因为你的浏览器中设置了一个 cookie;
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return(pass);
}
因此使用默认后端。
您需要在返回之前评估要使用哪个后端。