varnish config/docs (#342)
[akkoma] / docs / docs / configuration / optimisation / varnish_cache.md
1 # Using a Varnish Cache
2
3 Varnish is a layer that sits between your web server and your backend application -
4 it does something similar to nginx caching, but tends to be optimised for speed over
5 all else.
6
7 To set up a varnish cache, first you'll need to install varnish.
8
9 This will vary by distribution, and since this is a rather advanced guide,
10 no copy-paste instructions are provided. It's probably in your distribution's
11 package manager, though. `apt-get install varnish` and so on.
12
13 Once you have varnish installed, you'll need to configure it to work with akkoma.
14
15 Copy the configuration file to the varnish configuration directory:
16
17 cp installation/akkoma.vcl /etc/varnish/akkoma.vcl
18
19 You may want to check if varnish added a `default.vcl` file to the same directory,
20 if so you can just remove it without issue.
21
22 Then boot up varnish, probably `systemctl start varnish` or `service varnish start`.
23
24 Now you should be able to `curl -D- localhost:6081` and see a bunch of
25 akkoma javascript.
26
27 Once that's out of the way, we can point our webserver at varnish. This
28
29 === "Nginx"
30
31 upstream phoenix {
32 server 127.0.0.1:6081 max_fails=5 fail_timeout=60s;
33 }
34
35
36 === "Caddy"
37
38 reverse_proxy 127.0.0.1:6081
39
40 Now hopefully it all works
41
42 If you get a HTTPS redirect loop, you may need to remove this part of the VCL
43
44 ```vcl
45 if (std.port(server.ip) != 443) {
46 set req.http.X-Forwarded-Proto = "http";
47 set req.http.x-redir = "https://" + req.http.host + req.url;
48 return (synth(750, ""));
49 } else {
50 set req.http.X-Forwarded-Proto = "https";
51 }
52 ```
53
54 This will allow your webserver alone to handle redirects.