Merge branch 'develop' into stable
[akkoma] / installation / akkoma.vcl
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
3 vcl 4.1;
4 import std;
5
6 backend default {
7 .host = "127.0.0.1";
8 .port = "4000";
9 }
10
11 # ACL for IPs that are allowed to PURGE data from the cache
12 acl purge {
13 "127.0.0.1";
14 }
15
16 sub vcl_recv {
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, ""));
22 } else {
23 set req.http.X-Forwarded-Proto = "https";
24 }
25
26 # Pipe if WebSockets request is coming through
27 if (req.http.upgrade ~ "(?i)websocket") {
28 return (pipe);
29 }
30
31 # Allow purging of the cache
32 if (req.method == "PURGE") {
33 if (!client.ip ~ purge) {
34 return (synth(405,"Not allowed."));
35 }
36 return (purge);
37 }
38 }
39
40 sub vcl_backend_response {
41 # gzip text content
42 if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
43 set beresp.do_gzip = true;
44 }
45
46 # Retry broken backend responses.
47 if (beresp.status == 503) {
48 set bereq.http.X-Varnish-Backend-503 = "1";
49 return (retry);
50 }
51
52 # Bypass cache for large files
53 # 50000000 ~ 50MB
54 if (std.integer(beresp.http.content-length, 0) > 50000000) {
55 set beresp.uncacheable = true;
56 return (deliver);
57 }
58
59 # Don't cache objects that require authentication
60 if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
61 set beresp.uncacheable = true;
62 return (deliver);
63 }
64
65 # Allow serving cached content for 6h in case backend goes down
66 set beresp.grace = 6h;
67
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;
71 return (abandon);
72 }
73
74 # Do not cache redirects and errors
75 if ((beresp.status >= 300) && (beresp.status < 500)) {
76 set beresp.uncacheable = true;
77 set beresp.ttl = 30s;
78 return (deliver);
79 }
80 }
81
82 # The synthetic response for 301 redirects
83 sub vcl_synth {
84 if (resp.status == 750) {
85 set resp.status = 301;
86 set resp.http.Location = req.http.x-redir;
87 return (deliver);
88 }
89 }
90
91 # Ensure WebSockets through the pipe do not close prematurely
92 sub vcl_pipe {
93 if (req.http.upgrade) {
94 set bereq.http.upgrade = req.http.upgrade;
95 set bereq.http.connection = req.http.connection;
96 }
97 }
98
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;
103 }
104
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;
109 } else {
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;
116 } else {
117 return (abandon);
118 }
119 }
120 }
121 }
122
123 sub vcl_backend_error {
124 # Retry broken backend responses.
125 set bereq.http.X-Varnish-Backend-503 = "1";
126 return (retry);
127 }