105 lines
3.0 KiB
Plaintext
105 lines
3.0 KiB
Plaintext
vcl 4.1;
|
|
|
|
# https://github.com/varnish/toolbox/tree/master/vcls/hit-miss
|
|
include "hit-miss.vcl";
|
|
import std;
|
|
|
|
backend default {
|
|
.host = "rstat-dashboard";
|
|
.port = "5000";
|
|
}
|
|
|
|
sub vcl_recv {
|
|
if (req.method != "GET" &&
|
|
req.method != "HEAD" &&
|
|
req.method != "PUT" &&
|
|
req.method != "POST" &&
|
|
req.method != "TRACE" &&
|
|
req.method != "OPTIONS" &&
|
|
req.method != "DELETE") {
|
|
/* Non-RFC2616 or CONNECT which is weird. */
|
|
return (pipe);
|
|
}
|
|
|
|
# We only deal with GET and HEAD by default
|
|
if (req.method != "GET" && req.method != "HEAD") {
|
|
return (pass);
|
|
}
|
|
|
|
set req.url = regsub(req.url, "^http[s]?://", "");
|
|
|
|
# static files are always cacheable. remove SSL flag and cookie
|
|
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
|
|
unset req.http.Https;
|
|
unset req.http.X-Forwarded-Proto;
|
|
unset req.http.Cookie;
|
|
unset req.http.css;
|
|
unset req.http.js;
|
|
}
|
|
|
|
return (hash);
|
|
}
|
|
|
|
sub vcl_hash {
|
|
if (req.http.host) {
|
|
hash_data(req.http.host);
|
|
} else {
|
|
hash_data(server.ip);
|
|
}
|
|
|
|
# To make sure http users don't see ssl warning
|
|
if (req.http.X-Forwarded-Proto) {
|
|
hash_data(req.http.X-Forwarded-Proto);
|
|
}
|
|
}
|
|
|
|
sub vcl_backend_response {
|
|
set beresp.http.X-Host = bereq.http.host;
|
|
|
|
set beresp.ttl = 1m;
|
|
# Enable stale content serving
|
|
set beresp.grace = 24h;
|
|
# Preserve the origin's Cache-Control header for client-side caching
|
|
if (beresp.http.Cache-Control) {
|
|
set beresp.http.X-Orig-Cache-Control = beresp.http.Cache-Control;
|
|
}
|
|
|
|
# validate if we need to cache it and prevent from setting cookie
|
|
# images, css and js are cacheable by default so we have to remove cookie also
|
|
if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
|
|
unset beresp.http.set-cookie;
|
|
unset beresp.http.set-css;
|
|
unset beresp.http.set-js;
|
|
if (bereq.url !~ "\.(ico|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") {
|
|
set beresp.http.Pragma = "no-cache";
|
|
set beresp.http.Expires = "-1";
|
|
set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
|
|
set beresp.grace = 1m;
|
|
}
|
|
}
|
|
|
|
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
|
|
if (beresp.ttl <= 0s ||
|
|
beresp.http.Surrogate-control ~ "no-store" ||
|
|
(!beresp.http.Surrogate-Control && beresp.http.Vary == "*")) {
|
|
# Mark as Hit-For-Pass for the next 2 minutes
|
|
set beresp.ttl = 120s;
|
|
set beresp.uncacheable = true;
|
|
}
|
|
return (deliver);
|
|
}
|
|
|
|
sub vcl_deliver {
|
|
# Restore the origin's Cache-Control header for the browser
|
|
if (resp.http.X-Orig-Cache-Control) {
|
|
set resp.http.Cache-Control = resp.http.X-Orig-Cache-Control;
|
|
unset resp.http.X-Orig-Cache-Control;
|
|
} else {
|
|
# If no Cache-Control was set by the origin, we'll set a default
|
|
set resp.http.Cache-Control = "no-cache, must-revalidate";
|
|
}
|
|
|
|
unset resp.http.Server;
|
|
unset resp.http.Via;
|
|
unset resp.http.Link;
|
|
} |