I often find myself looking for the same Caddy web-server configurations for a sane and sensible setup for most projects.
Here are some of the bits I've collected and use (Please excuse the missing syntax highlighting. The Codeberg link does a better job.):(caddy-sensible-defaults) {
import caddy-sensible-defaults-v1.2
}
(caddy-sensible-defaults-v1.2) {
# Redirect www.
@www header_regexp www Host ^www\.(.*)$
redir @www https://{re.www.1} permanent
# Compress if wanted.
encode
# Hidden Files, but not well-known API.
@forbidden_dotfiles {
not path /.well-known/*
path */.*
}
error @forbidden_dotfiles 404
# Error Pages
# https://caddyserver.com/docs/caddyfile/directives/handle_errors#examples
# `handle_errors` is special does not respect root path, from the route it's included in. https://caddy.community/t/error-when-using-handle-errors-inside-route/13143/2
handle_errors {
# does this exist?
root /srv/http-errors/
@custom_err file {err.status_code}.html
handle @custom_err {
rewrite * {file_match.relative}
file_server
}
# if not, text error page.
respond "{err.status_code} {err.status_text}"
}
# Outgoing Security Headers & CORs
header {
# Remove Server info
-Server
# Remove Via (similar to Server header)
-Via
# Proxy Authentication Key
-Via-Key
# Remove X-Powered-By (Framework name)
-X-Powered-By
# HTTP Security Headers #
#########################
# i.e. Reducing attack surface and enforcing browser security behaviors.
# `?` means, set, if not already set.
# CSP with a weak but largely compatible config. Should be tightened by backends where possible.
?Content-Security-Policy "default-src 'self' 'unsafe-inline' https:; frame-ancestors 'self'; form-action https:; base-uri 'self'; upgrade-insecure-requests; block-all-mixed-content;"
# Always use HTTPS? Enable if wanted by removing `#`.
#?Strict-Transport-Security "max-age=31536000; includeSubDomains"
# MIME types advertised in the Content-Type headers should be respected and not changed.
?X-Content-Type-Options "nosniff"
# Content-Security-Policy header supersedes X-Frame-Options, but kept for backwards compatibility.
?X-Frame-Options "SAMEORIGIN"
# Controls how much referrer information in the Referrer header. Full URL for same-origin, only origin for cross-origin at the same security level, omits on a protocol downgrade.
?Referrer-Policy "strict-origin-when-cross-origin"
# Disable these permissons.
?Permissions-Policy "camera=(), microphone=(), geolocation=(), usb=(), interest-cohort=()"
# CORS (Cross-Origin Resource Sharing) #
########################################
# Anyone can consume your resources.
?Access-Control-Allow-Origin "*"
# CORP (i.e. who can load this resource?)
?Cross-Origin-Resource-Policy "cross-origin"
# COOP (i.e. how your page relates to other tabs or windows.)
# Only same origin can share a window context/tab process.
?Cross-Origin-Opener-Policy "same-origin"
# COEP (i.e. which external resources your page is allowed to load.)
# Blocks cross-origin resources, unless CORP header or CORS setup.
?Cross-Origin-Embedder-Policy "require-corp"
}
# Cache Controls for most common static assets
@static_cache {
file
path /assets/* *.css *.js *.ico *.gif *.jpg *.jpeg *.png *.svg *.webp *.woff *.woff2 *.ttf *.otf *.pdf *.mp4 *.webm *.ogg *.ogv *.mp3 *.wav *.aac
}
handle @static_cache {
# `?` = passively set header if none upstream. 7 days.
header ?Cache-Control "public, max-age=604800, must-revalidate"
}
}
Contribute to this snippet on Codeberg: sensible-caddy-config/sensible-defaults.Caddyfile.
For the www redirection to work, your site handler must also include the www subdomain. You'll need to setup the DNS for it too. I recommend just setting www as a CNAME to @ on your NS.
Allow compression. Hide dotfiles like Apache 2.
Look for existing HTML error pages at the sites route, /error/HTTP_STATUS_CODE.html, or output a text error.
Set and remove some sensible headers. (I use a non-standard Via-Key to trust proxies within an app, you likely don't need to worry about removing this from response headers.)
Then setup some sensible cache settings for static assets, but only if the app itself has not set them already.
Why the double import? I find it easier to embed the version number into the import name so it's clear what version of sensible Caddy defaults I'm running - but I don't want to be updating loads of site handlers every version update!
Example of usage while reverse proxying to a php app running in a container in the same docker-compose file (same network):
test.internal www.test.internal {
root * /srv/dist/public
import caddy-sensible-defaults
# Enable the static file server.
file_server
# Serve PHP files via php-fpm.
php_fastcgi php:9000
}
2026-08-25: Updated with generic HTML error pages and some other upgrades, including a changelog!# CHANGELOG
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) ish.
## [Unreleased]
- [ ] Internal SSL for internal domains.
## [1.2] - 2026-08-25
### Added
- CHANGELOG.md
- Generic HTML Error (HTTP Status) Pages
- For the most common HTTP statuses, using some text from https://restfulapi.net/http-status-codes/ and Bootstrap v5.3.8 via a Bunny HopJS CDN.
- 400 Bad Request
- 401 Unauthorized403 Forbidden404 Not Found
- 429 Too Many Requests
- 500 Internal Server Error
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
### Fixed
- Generic Error pages would not respect route's root path.
### Changed
- Upgraded security headers, although they're still soft for compatibility.
- Added more common static asset file extensions to 7 days cache.
## [1] - 2026-02-24
### Added
- init
Contribute to this snippet on Codeberg: sensible-caddy-config/CHANGELOG.md.
Comments & Questions
to send in your thoughts.
Comments may be featured here unless you say otherwise. You can encrypt emails with PGP too, learn more about my email replies here.
PGP: 9ba2c5570aec2933970053e7967775cb1020ef23