Caddy Server & Caddyfile Cheat Sheet
Quick reference guide for Caddy Server: Caddyfile directives, reverse proxy, automatic HTTPS, and headers.
Web & Network
caddy
caddyfile
webserver
Caddy is a powerful, enterprise-ready, open-source web server with automatic HTTPS written in Go.
Caddyfile Basic Directives
caddy
# Automatic HTTPS for domain
example.com {
# Reverse proxy to local backend server
reverse_proxy localhost:8080
# Custom Response Headers
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains"
X-Content-Type-Options "nosniff"
-Server # Strip Caddy Server header
}
# Enable Gzip & Zstd Compression
encode zstd gzip
}
# Static File Server for Single Page App (SPA)
app.example.com {
root * /var/www/html
file_server
try_files {path} /index.html
}
Common Directives Summary
Table
| Directive | Purpose | Example Usage |
|---|---|---|
reverse_proxy | Proxy traffic to backend IP/port | reverse_proxy 127.0.0.1:3000 |
root | Set root folder for static file server | root * /srv/public |
file_server | Enable static file server browsing | file_server browse |
redir | Redirect HTTP requests to new URL | redir https://newdomain.com{uri} permanent |
encode | Enable response compression | encode zstd gzip |
rewrite | Rewrite URI path internally | @api path_regexp api ^/api/(.*)$<br>rewrite @api /v1/{re.api.1} |
basic_auth | Protect endpoint with HTTP Basic Auth | basic_auth { user hashed_pass } |
Essential Caddy CLI Commands
Table
| Command | Action / Purpose |
|---|---|
caddy run | Run Caddy in foreground (blocking) |
caddy start | Start Caddy daemon process in background |
caddy stop | Gracefully stop running Caddy background process |
caddy reload | Reload Caddyfile configuration without dropping connections |
caddy fmt | Format Caddyfile according to standard formatting style |
caddy validate | Test Caddyfile syntax for errors without applying changes |
Common Pitfalls & Tips
[!WARNING] Testing Caddy in local development against public domain names can hit Let's Encrypt rate limits! Use
tls internalfor local development domains.
[!TIP] Use
caddy fmt --overwriteto clean up indentation and directive structure in yourCaddyfileautomatically.