ずっとCookieのターンなのでうまく使えない。知見ほしい。
とりあえず画像とかcss、jsだけキャッシュするようにして、
varnish -d -f /etc/varnish/default.vcl -C して入れた。
backend default {
.host = "127.0.0.1";
.port = "8080";
}
acl local {
"localhost"; // 自分自身
"会社のIPアドレス";
}
# ログを使うときに外す stg.log("hogehge");
#import std;
sub vcl_recv {
if (req.request == "PURGE") {
ban("req.http.host == " + req.http.host + "&& req.url == "+ req.url);
error 200 "banned.";
}
if (req.request !~ "^(GET|HEAD|PUT|POST|TRACE|OPTIONS|DELETE)$") {
return(pipe); //不明なメソッドの通信はパイプする
} elseif (req.url ~ "^/admin/") {
if (client.ip !~ local) {
error 403 "Forbidden";
} else {
return(pass); //admin ページはキャッシュしない
}
} elseif (req.request !~ "^(GET|HEAD)$") {
return(pass); //GET/HEAD以外はキャッシュしない
} else {
if (req.url ~ "\.(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf|woff)$") {
unset req.http.Cookie;
}
# クッキーセットしておかないと死ぬ
# if (req.http.X-Requested-With != "XMLHttpRequest") {
# unset req.http.Cookie;
# }
return(lookup); //キャッシュの動作をする
}
}
sub vcl_fetch {
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
return (hit_for_pass);
}
if (beresp.status == 404) {
set beresp.ttl = 1m;
}
return (deliver);
}