Caddy configuration for Django with some sensible defaults

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.

Caddy configuration for Django with some sensible defaults
Isaac Bythewood Isaac Bythewood
2022-06-04

Caddy 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.

As an explanation this Caddyfile will do a few things, starting from the top:

  • Create a (common) configuration to import into all my domains
  • XSS protection header
  • Content sniffing protection header
  • Caching with a max age of two months
  • Encoding starting with the better zstd and falling back to the widely supported gzip
  • Handling for serving my media files such as images and documents
  • And finally a reverse_proxy to my gunicorn server

Caddy 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.

For more information Caddy and more settings check out Caddy's website.


Some posts in similar tags to this one.

Finding broken external links on websites using Scrapy
Finding broken external links on websites using Scrapy
Broken links are a problem for any content driven website as it ages, find them quickly and easily with Scrapy.
Isaac Bythewood Isaac Bythewood
2022-07-23
Using CodeMirror to show formatted code in Wagtail
Using CodeMirror to show formatted code in Wagtail
Going through all the steps to use CodeMirror with Wagtail to show formatted code on the frontend of your site.
Isaac Bythewood Isaac Bythewood
2022-06-11
Managing a Python project with uv in 2026
Managing a Python project with uv in 2026
How I set up and manage Python projects in 2026 using uv and ruff. Two tools and one config file replace everything.
Isaac Bythewood Isaac Bythewood
2026-04-05