diff --git a/deploy/varnish/default.vcl.template b/deploy/varnish/default.vcl.template index e1382c9..d0f7d87 100644 --- a/deploy/varnish/default.vcl.template +++ b/deploy/varnish/default.vcl.template @@ -39,7 +39,10 @@ sub vcl_recv { # metody inne niż GET/HEAD bez cache if (req.method != "GET" && req.method != "HEAD") { return (pass); } - # ---- Normalizacja Accept-Encoding (kolejność preferencji: zstd > br > gzip) ---- + # Żądania z Authorization nie są buforowane + if (req.http.Authorization) { return (pass); } + + # ---- Normalizacja Accept-Encoding (kolejność: zstd > br > gzip) ---- if (req.http.Accept-Encoding) { if (req.http.Accept-Encoding ~ "zstd") { set req.http.Accept-Encoding = "zstd"; @@ -53,9 +56,6 @@ sub vcl_recv { } # ---- (Opcjonalnie) Normalizacja Accept dla obrazów generowanych wariantowo ---- - # Jeśli backend renderuje różne formaty obrazów (webp/jpg) pod tym samym URL (bez rozszerzenia), - # można włączyć „dwustanowy” sygnał do hasha: - # # if (req.url ~ "\.(png|jpe?g|gif|bmp)$") { # if (req.http.Accept ~ "image/webp") { # set req.http.X-Accept-Image = "modern"; # webp @@ -71,6 +71,10 @@ sub vcl_recv { return (hash); } + if (!req.http.X-Forwarded-Proto) { + set req.http.X-Forwarded-Proto = "https"; + } + return (hash); } @@ -106,6 +110,42 @@ sub vcl_backend_response { return (deliver); } + # NIE cache'uj redirectów do loginu (HTML) z backendu + if (beresp.status >= 300 && beresp.status < 400) { + set beresp.uncacheable = true; + set beresp.ttl = 0s; + return (deliver); + } + + # Nie cache'uj statyków, jeśli status ≠ 200 + if (bereq.url ~ "^/static/" || + bereq.url ~ "\.(css|js|png|jpe?g|webp|svg|ico|woff2?)($|\?)") { + if (beresp.status != 200) { + set beresp.uncacheable = true; + set beresp.ttl = 0s; + return (deliver); + } + } + + # Jeśli pod .js przychodzi text/html — też nie cache'uj (to zwykle redirect/login) + if (bereq.url ~ "\.js(\?.*)?$" && beresp.http.Content-Type ~ "(?i)text/html") { + set beresp.uncacheable = true; + set beresp.ttl = 0s; + return (deliver); + } + + # Wymuś poprawny Content-Type dla .js/.css, gdy backend zwróci HTML + if (bereq.url ~ "\.js(\?.*)?$") { + if (!beresp.http.Content-Type || beresp.http.Content-Type ~ "(?i)text/html") { + set beresp.http.Content-Type = "application/javascript; charset=utf-8"; + } + } + if (bereq.url ~ "\.css(\?.*)?$") { + if (!beresp.http.Content-Type || beresp.http.Content-Type ~ "(?i)text/html") { + set beresp.http.Content-Type = "text/css; charset=utf-8"; + } + } + # ---- STATYCZNE: zdejmij Set-Cookie i Vary: Cookie, zapewnij TTL ---- if (bereq.url ~ "^/static/" || bereq.url ~ "\.(css|js|png|jpe?g|webp|svg|ico|woff2?)$") { unset beresp.http.Set-Cookie; @@ -150,8 +190,8 @@ sub vcl_backend_response { # Kompresja po stronie Varnisha wyłącznie dla klientów akceptujących gzip # i tylko jeśli backend nie dostarczył już Content-Encoding. if (!beresp.http.Content-Encoding && bereq.http.Accept-Encoding ~ "gzip") { - # Kompresujemy tylko „tekstowe” typy - if (beresp.http.Content-Type ~ "(?i)text/|application/(javascript|json|xml|wasm)") { + # Kompresujemy tylko „tekstowe” typy; wykluczamy WASM + if (beresp.http.Content-Type ~ "(?i)text/|application/(javascript|json|xml)") { set beresp.do_gzip = true; } } @@ -162,6 +202,11 @@ sub vcl_backend_response { } } +# (Opcjonalnie) Serwuj „stale” przy błędach backendu, jeśli jest obiekt w grace +sub vcl_backend_error { + return (deliver); +} + # ===== DELIVER ===== sub vcl_deliver { if (obj.uncacheable) { @@ -187,4 +232,3 @@ sub vcl_synth { sub vcl_purge { return (synth(200, "Purged")); } -