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
DirectivePurposeExample Usage
reverse_proxyProxy traffic to backend IP/portreverse_proxy 127.0.0.1:3000
rootSet root folder for static file serverroot * /srv/public
file_serverEnable static file server browsingfile_server browse
redirRedirect HTTP requests to new URLredir https://newdomain.com{uri} permanent
encodeEnable response compressionencode zstd gzip
rewriteRewrite URI path internally@api path_regexp api ^/api/(.*)$<br>rewrite @api /v1/{re.api.1}
basic_authProtect endpoint with HTTP Basic Authbasic_auth { user hashed_pass }

Essential Caddy CLI Commands

Table
CommandAction / Purpose
caddy runRun Caddy in foreground (blocking)
caddy startStart Caddy daemon process in background
caddy stopGracefully stop running Caddy background process
caddy reloadReload Caddyfile configuration without dropping connections
caddy fmtFormat Caddyfile according to standard formatting style
caddy validateTest 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 internal for local development domains.

[!TIP] Use caddy fmt --overwrite to clean up indentation and directive structure in your Caddyfile automatically.