varnish 設定

sub vcl_recv {
# no Get or HEAD method
if (req.request != "GET" && req.request != "HEAD") {
return(pipe);
}
# no-cache
if (req.http.Cache-Control ~ "no-cache") {
return(pass);
}
return(lookup);
}

sub vcl_fetch {
if (beresp.status == 500) {
set beresp.saintmode = 10s;
return(restart);
}
set beresp.grace = 5m;
if (req.url ~ "\.(png|gif|jpg|jpeg|css|js|ico)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 3600s;
}
}

1. GET or HEAD メソッド以外はコンテンツサーバに任せる
2. Cache-Control ヘッダがついている場合はコンテンツサーバに任せる
3. コンテンツサーバが 500 レスポンスをしている場合は、10秒まって再度リクエス
4. 静的コンテンツの url の場合、1時間キャッシュする。