Merge remote-tracking branch 'upstream/develop' into patch-image-description
[akkoma] / installation / pleroma.vcl
1 vcl 4.0;
2 import std;
3
4 backend default {
5 .host = "127.0.0.1";
6 .port = "4000";
7 }
8
9 # ACL for IPs that are allowed to PURGE data from the cache
10 acl purge {
11 "127.0.0.1";
12 }
13
14 sub vcl_recv {
15 # Redirect HTTP to HTTPS
16 if (std.port(server.ip) != 443) {
17 set req.http.x-redir = "https://" + req.http.host + req.url;
18 return (synth(750, ""));
19 }
20
21 # CHUNKED SUPPORT
22 if (req.http.Range ~ "bytes=") {
23 set req.http.x-range = req.http.Range;
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 # Pleroma MediaProxy - strip headers that will affect caching
40 if (req.url ~ "^/proxy/") {
41 unset req.http.Cookie;
42 unset req.http.Authorization;
43 unset req.http.Accept;
44 return (hash);
45 }
46
47 # Strip headers that will affect caching from all other static content
48 # This also permits caching of individual toots and AP Activities
49 if ((req.url ~ "^/(media|static)/") ||
50 (req.url ~ "(?i)\.(html|js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$"))
51 {
52 unset req.http.Cookie;
53 unset req.http.Authorization;
54 return (hash);
55 }
56 }
57
58 sub vcl_backend_response {
59 # gzip text content
60 if (beresp.http.content-type ~ "(text|text/css|application/x-javascript|application/javascript)") {
61 set beresp.do_gzip = true;
62 }
63
64 # CHUNKED SUPPORT
65 if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) {
66 set beresp.ttl = 10m;
67 set beresp.http.CR = beresp.http.content-range;
68 }
69
70 # Don't cache objects that require authentication
71 if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") {
72 set beresp.uncacheable = true;
73 return (deliver);
74 }
75
76 # Default object caching of 86400s;
77 set beresp.ttl = 86400s;
78 # Allow serving cached content for 6h in case backend goes down
79 set beresp.grace = 6h;
80
81 # Do not cache 5xx responses
82 if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
83 set beresp.uncacheable = true;
84 return (abandon);
85 }
86
87 # Do not cache redirects and errors
88 if ((beresp.status >= 300) && (beresp.status < 500)) {
89 set beresp.uncacheable = true;
90 set beresp.ttl = 30s;
91 return (deliver);
92 }
93
94 # Pleroma MediaProxy internally sets headers properly
95 if (bereq.url ~ "^/proxy/") {
96 return (deliver);
97 }
98
99 # Strip cache-restricting headers from Pleroma on static content that we want to cache
100 if (bereq.url ~ "(?i)\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$")
101 {
102 unset beresp.http.set-cookie;
103 unset beresp.http.Cache-Control;
104 unset beresp.http.x-request-id;
105 set beresp.http.Cache-Control = "public, max-age=86400";
106 }
107 }
108
109 # The synthetic response for 301 redirects
110 sub vcl_synth {
111 if (resp.status == 750) {
112 set resp.status = 301;
113 set resp.http.Location = req.http.x-redir;
114 return(deliver);
115 }
116 }
117
118 # Ensure WebSockets through the pipe do not close prematurely
119 sub vcl_pipe {
120 if (req.http.upgrade) {
121 set bereq.http.upgrade = req.http.upgrade;
122 set bereq.http.connection = req.http.connection;
123 }
124 }
125
126 sub vcl_hash {
127 # CHUNKED SUPPORT
128 if (req.http.x-range ~ "bytes=") {
129 hash_data(req.http.x-range);
130 unset req.http.Range;
131 }
132 }
133
134 sub vcl_backend_fetch {
135 # CHUNKED SUPPORT
136 if (bereq.http.x-range) {
137 set bereq.http.Range = bereq.http.x-range;
138 }
139 }
140
141 sub vcl_deliver {
142 # CHUNKED SUPPORT
143 if (resp.http.CR) {
144 set resp.http.Content-Range = resp.http.CR;
145 unset resp.http.CR;
146 }
147 }