1 # Recommended varnishncsa logging format: '%h %l %u %t "%m %{X-Forwarded-Proto}i://%{Host}i%U%q %H" %s %b "%{Referer}i" "%{User-agent}i"'
2 # Please use Varnish 7.0+ for proper Range Requests / Chunked encoding support
11 # ACL for IPs that are allowed to PURGE data from the cache
17 # Redirect HTTP to HTTPS
18 if (std.port(server.ip) != 443) {
19 set req.http.X-Forwarded-Proto = "http";
20 set req.http.x-redir = "https://" + req.http.host + req.url;
21 return (synth(750, ""));
23 set req.http.X-Forwarded-Proto = "https";
26 # Pipe if WebSockets request is coming through
27 if (req.http.upgrade ~ "(?i)websocket") {
31 # Allow purging of the cache
32 if (req.method == "PURGE") {
33 if (!client.ip ~ purge) {
34 return (synth(405,"Not allowed."));
40 sub vcl_backend_response {
42 if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
43 set beresp.do_gzip = true;
46 # Retry broken backend responses.
47 if (beresp.status == 503) {
48 set bereq.http.X-Varnish-Backend-503 = "1";
52 # Bypass cache for large files
54 if (std.integer(beresp.http.content-length, 0) > 50000000) {
55 set beresp.uncacheable = true;
59 # Don't cache objects that require authentication
60 if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
61 set beresp.uncacheable = true;
65 # Allow serving cached content for 6h in case backend goes down
66 set beresp.grace = 6h;
68 # Do not cache 5xx responses
69 if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
70 set beresp.uncacheable = true;
74 # Do not cache redirects and errors
75 if ((beresp.status >= 300) && (beresp.status < 500)) {
76 set beresp.uncacheable = true;
82 # The synthetic response for 301 redirects
84 if (resp.status == 750) {
85 set resp.status = 301;
86 set resp.http.Location = req.http.x-redir;
91 # Ensure WebSockets through the pipe do not close prematurely
93 if (req.http.upgrade) {
94 set bereq.http.upgrade = req.http.upgrade;
95 set bereq.http.connection = req.http.connection;
99 sub vcl_backend_fetch {
100 # Be more lenient for slow servers on the fediverse
101 if (bereq.url ~ "^/proxy/") {
102 set bereq.first_byte_timeout = 300s;
105 if (bereq.retries == 0) {
106 # Clean up the X-Varnish-Backend-503 flag that is used internally
107 # to mark broken backend responses that should be retried.
108 unset bereq.http.X-Varnish-Backend-503;
110 if (bereq.http.X-Varnish-Backend-503) {
111 if (bereq.method != "POST" &&
112 std.healthy(bereq.backend) &&
113 bereq.retries <= 4) {
114 # Flush broken backend response flag & try again.
115 unset bereq.http.X-Varnish-Backend-503;
123 sub vcl_backend_error {
124 # Retry broken backend responses.
125 set bereq.http.X-Varnish-Backend-503 = "1";