orchard
mirrorEvery site I host, in one repo, along with the Cloudflare Tunnel and Caddy that front them. It's all Go, Vite, and SQLite, and it runs on a desktop at home with nothing listening on an inbound port.
blogbuncaddycloudflare-tunneldockergogolanghomelabhtml-templatemonorepoself-hostedseosqlitestatic-sitetypstuptime-monitoringviteweb-analytics
1---
2title: Caddy configuration for Django with some sensible defaults
3slug: caddy-configuration-for-django-with-some-sensible-defaults
4date: 2022-06-04
5publish_date: 2022-06-04
6tags: coding, webdev
7description: Caddy is a great web server with sensible defaults but there a few things that I need to configure to have perfect synergy with Django.
8cover_image: caddyserver.com.webp
9---
10
11Caddy has become my favorite web server with its great default configuration and even better performance. I try to setup Django with as much security and performance as possible but there are a few things that I need to offload to my web server such as serving media files. So my default configuration has settled in around something like this.
12
13```shell
14(common) {
15 header /* {
16 X-XSS-Protection "1; mode=block"
17 X-Content-Type-Options nosniff
18 -Server
19 }
20
21 header /media/* {
22 Cache-Control "public, max-age=315360000"
23 }
24
25 encode zstd gzip
26}
27
28blog.example.com {
29 handle /media/* {
30 uri strip_prefix /media
31 file_server {
32 root /srv/data/blog/media
33 }
34 }
35
36 reverse_proxy localhost:8000
37
38 import common
39}
40```
41
42As an explanation this Caddyfile will do a few things, starting from the top:
43
44* Create a (common) configuration to import into all my domains
45* XSS protection header
46* Content sniffing protection header
47* Caching with a max age of two months
48* Encoding starting with the better zstd and falling back to the widely supported gzip
49* Handling for serving my media files such as images and documents
50* And finally a reverse\_proxy to my gunicorn server
51
52Caddy will by default create and enable HTTPS which is a huge benefit, I don't even have to consider security on that front. From here Django handles everything else such as setting cookie security and HSTS headers.
53
54For more information Caddy and more settings check out [Caddy's website](https://caddyserver.com/).