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 website
Author
Isaac Bythewood Isaac Bythewood
June 04, 2022

Caddy has become my favorite web server with it's 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.


Related posts

Some posts in similar tags to this one.

EditorConfig website
Using EditorConfig to improve coding style consistency
EditorConfig has been around for almost a decade at this point. It is widely supported by many editors natively and many more with plugins.
Isaac Bythewood Isaac Bythewood
May 21, 2022
Django Dockerfile
Running a simple Django website in Docker
Using Docker to run a simple production and development environments with a few extras thrown in. Easily customized to your preferred language or framework.
Isaac Bythewood Isaac Bythewood
May 14, 2022
CodeMirror website
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
June 11, 2022
© 2024 Isaac Bythewood. Some rights reserved.