From 68a7e07c58eaa1c69f00185df62d7c6fca72eb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Thu, 25 Sep 2025 10:28:55 +0200 Subject: [PATCH] varnish reconfig --- app.py | 1 + deploy/varnish/default.vcl.template | 63 +++++++++++++++-------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/app.py b/app.py index d0ac194..d859302 100644 --- a/app.py +++ b/app.py @@ -1385,6 +1385,7 @@ def require_system_password(): "static_bp.serve_css_lib", "favicon", "favicon_ico", + "uploaded_file" ): return diff --git a/deploy/varnish/default.vcl.template b/deploy/varnish/default.vcl.template index 345251f..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 @@ -108,6 +108,7 @@ sub vcl_backend_response { set beresp.uncacheable = true; set beresp.ttl = 0s; return (deliver); + } # NIE cache'uj redirectów do loginu (HTML) z backendu if (beresp.status >= 300 && beresp.status < 400) { @@ -116,35 +117,33 @@ sub vcl_backend_response { 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") { + # 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); } + } - # 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"; - } - } + # 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 ---- @@ -191,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; } } @@ -203,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) { @@ -228,4 +232,3 @@ sub vcl_synth { sub vcl_purge { return (synth(200, "Purged")); } -