From cc1f1e1ad70efa539e26232382afa90a973b4aae Mon Sep 17 00:00:00 2001 From: caskd Date: Sat, 14 Mar 2020 14:02:07 +0100 Subject: [PATCH] Add more advanced cache handling --- build/Varnish/varnish.vcl | 45 +++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/build/Varnish/varnish.vcl b/build/Varnish/varnish.vcl index 9357389..307d057 100644 --- a/build/Varnish/varnish.vcl +++ b/build/Varnish/varnish.vcl @@ -9,11 +9,48 @@ backend default { .between_bytes_timeout = 2s; .proxy_header = 2; } +sub vcl_recv { + if ( req.method != "GET" && + req.method != "HEAD" && + req.method != "PUT" && + req.method != "POST" && + req.method != "TRACE" && + req.method != "OPTIONS" && + req.method != "PATCH" && + req.method != "DELETE") { + return (pipe); + } + if (req.http.Upgrade ~ "(?i)websocket") { + return (pipe); + } + if (req.method != "GET" && req.method != "HEAD") { + return (pass); + } + return (hash); +} +sub vcl_hit { + if (obj.ttl + obj.grace > 0s) { + return (deliver); + } + return (pass); +} +sub vcl_pipe { + if (req.http.upgrade) { + set bereq.http.upgrade = req.http.upgrade; + } + return (pipe); +} +sub vcl_hash { + hash_data(req.url); + if (req.http.Cookie) { + hash_data(req.http.Cookie); + } +} sub vcl_backend_response { - set beresp.ttl = 5m; - if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") { - unset beresp.http.set-cookie; - set beresp.do_stream = true; + set beresp.grace = 1m; + set beresp.keep = 4m; + if (beresp.http.ETag || beresp.http.Last-Modified) { + set beresp.keep = 4h; } return (deliver); }