4752510ea010be1c91abb14bbf947d433d046131
[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 vcl 4.1;
3 import std;
4
5 backend default {
6 .host = "127.0.0.1";
7 .port = "4000";
8 }
9
10 # ACL for IPs that are allowed to PURGE data from the cache
11 acl purge {
12 "127.0.0.1";
13 }
14
15 sub vcl_recv {
16 # Redirect HTTP to HTTPS
17 if (std.port(server.ip) != 443) {
18 set req.http.X-Forwarded-Proto = "http";
19 set req.http.x-redir = "https://" + req.http.host + req.url;
20 return (synth(750, ""));
21 } else {
22 set req.http.X-Forwarded-Proto = "https";
23 }
24
25 # CHUNKED SUPPORT
26 if (req.http.Range ~ "bytes=") {
27 set req.http.x-range = req.http.Range;
28 }
29
30 # Pipe if WebSockets request is coming through
31 if (req.http.upgrade ~ "(?i)websocket") {
32 return (pipe);
33 }
34
35 # Allow purging of the cache
36 if (req.method == "PURGE") {
37 if (!client.ip ~ purge) {
38 return(synth(405,"Not allowed."));
39 }
40 return(purge);
41 }
42 }
43
44 sub vcl_backend_response {
45 # gzip text content
46 if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
47 set beresp.do_gzip = true;
48 }
49
50 # Retry broken backend responses.
51 if (beresp.status == 503) {
52 set bereq.http.X-Varnish-Backend-503 = "1";
53 return (retry);
54 }
55
56 # CHUNKED SUPPORT
57 if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) {
58 set beresp.ttl = 10m;
59 set beresp.http.CR = beresp.http.content-range;
60 }
61
62 # Bypass cache for large files
63 # 50000000 ~ 50MB
64 if (std.integer(beresp.http.content-length, 0) > 50000000) {
65 set beresp.uncacheable = true;
66 return(deliver);
67 }
68
69 # Don't cache objects that require authentication
70 if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
71 set beresp.uncacheable = true;
72 return (deliver);
73 }
74
75 # Allow serving cached content for 6h in case backend goes down
76 set beresp.grace = 6h;
77
78 # Do not cache 5xx responses
79 if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
80 set beresp.uncacheable = true;
81 return (abandon);
82 }
83
84 # Do not cache redirects and errors
85 if ((beresp.status >= 300) && (beresp.status < 500)) {
86 set beresp.uncacheable = true;
87 set beresp.ttl = 30s;
88 return (deliver);
89 }
90 }
91
92 # The synthetic response for 301 redirects
93 sub vcl_synth {
94 if (resp.status == 750) {
95 set resp.status = 301;
96 set resp.http.Location = req.http.x-redir;
97 return(deliver);
98 }
99 }
100
101 # Ensure WebSockets through the pipe do not close prematurely
102 sub vcl_pipe {
103 if (req.http.upgrade) {
104 set bereq.http.upgrade = req.http.upgrade;
105 set bereq.http.connection = req.http.connection;
106 }
107 }
108
109 sub vcl_hash {
110 # CHUNKED SUPPORT
111 if (req.http.x-range ~ "bytes=") {
112 hash_data(req.http.x-range);
113 unset req.http.Range;
114 }
115 }
116
117 sub vcl_backend_fetch {
118 # Be more lenient for slow servers on the fediverse
119 if (bereq.url ~ "^/proxy/") {
120 set bereq.first_byte_timeout = 300s;
121 }
122
123 # CHUNKED SUPPORT
124 if (bereq.http.x-range) {
125 set bereq.http.Range = bereq.http.x-range;
126 }
127
128 if (bereq.retries == 0) {
129 # Clean up the X-Varnish-Backend-503 flag that is used internally
130 # to mark broken backend responses that should be retried.
131 unset bereq.http.X-Varnish-Backend-503;
132 } else {
133 if (bereq.http.X-Varnish-Backend-503) {
134 if (bereq.method != "POST" &&
135 std.healthy(bereq.backend) &&
136 bereq.retries <= 4) {
137 # Flush broken backend response flag & try again.
138 unset bereq.http.X-Varnish-Backend-503;
139 } else {
140 return (abandon);
141 }
142 }
143 }
144 }
145
146 sub vcl_deliver {
147 # CHUNKED SUPPORT
148 if (resp.http.CR) {
149 set resp.http.Content-Range = resp.http.CR;
150 unset resp.http.CR;
151 }
152 }
153
154 sub vcl_backend_error {
155 # Retry broken backend responses.
156 set bereq.http.X-Varnish-Backend-503 = "1";
157 return (retry);
158 }