有会 varnish 设置的吗?求找 bug 首次访问一定 miss

2014-05-16 12:46:04 +08:00
 baskice
https://github.com/moegirlwiki/varnish/blob/master/default.vcl
https://github.com/moegirlwiki/varnish/blob/master/devicedetect.vcl

配置文件如上,现在在新电脑上首次访问网页一定会miss,这不就等于没用了吗orz
1944 次点击
所在节点    问与答
2 条回复
rhwood
2014-05-16 14:14:07 +08:00
不考虑分类情况,一律unset req.http.Cookie后测试看看
rails3
2014-05-16 15:21:11 +08:00
这是我们一个可以使用的配置,varnish 3.x

backend default {
.host = "172.x.x.x";
.port = "8080";
}

sub vcl_recv {

if (req.request == "CACHEPURGE") {
ban("req.http.host == " + regsub(req.http.host, ":6081", "") + " && req.url == " + req.url);
error 200 "Ban added";
}

if (req.request == "CACHEPURGEDIRECTORY") {
ban("req.http.host == " + regsub(req.http.host, ":6081", "") + " && req.url ~ " + req.url);
error 200 "Ban added";
}

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;
}
}

set req.backend = default;

if (req.http.host == "xxxx.com") {
set req.http.host = "www.xxxx.com";
}

if (req.url ~ "\.(jsp|action)$") {
return (pass);
}

if (req.url ~ "(.*)/(\?.*)?$") {
set req.url = regsub(req.url, "(.*)/(\?.*)?$", "\1/");
}

if (req.url ~ "\.html?.*") {
set req.url = regsub(req.url, "\.html?.*", "\.html");
}

if (req.url ~ "/index.html$") {
set req.url = regsub(req.url, "/index.html", "/");
}

if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|htm|html)$") {
remove req.http.Accept-Encoding;
unset req.http.Cookie;
unset req.http.Vary;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}


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.request == "xxx" || req.request == "xxx_directory") {
purge;
error 200 "Purged.";
}
return (deliver);
}

sub vcl_miss {
if (req.request == "xxx" || req.request == "xxx_directory") {
purge;
error 200 "Purged.";
}
return (fetch);
}

sub vcl_fetch {

if (req.http.host == "x.xxxx.com") {
unset beresp.http.set-cookie;
set beresp.ttl = 7d;
}

if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
set beresp.ttl = 120 s;
return (hit_for_pass);
}

if (beresp.status == 404 || beresp.status == 503 || beresp.status == 500 || beresp.status == 502) {
set beresp.http.X-Cacheable = "NO: beresp.status";
set beresp.http.X-Cacheable-status = beresp.status;
return (hit_for_pass);
}

if (req.url ~ "\.(html|htm)$") {
set beresp.do_gzip = true;
if (req.url ~ "/list_") {
set beresp.ttl = 600s;
} else {
set beresp.ttl = 8h;
}
} else if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf)$") {
set beresp.ttl = 8h;
} else {
set beresp.ttl = 8h;
}

return (deliver);
}

sub vcl_deliver {
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT From Server!";
} else {
set resp.http.X-Cache = "MISS Me!";
}

remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.Server;
remove resp.http.X-Powered-By;

return (deliver);
}

sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>xxxx cache server</p>
</body>
</html>
"};
return (deliver);
}

sub vcl_init {
return (ok);
}

sub vcl_fini {
return (ok);
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/112821

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX