The Caddyfile is Caddy’s native configuration format - a human-friendly alternative to JSON. It provides a simple, intuitive syntax for configuring your web server.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/caddyserver/caddy/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Caddyfile adapter converts Caddyfile syntax to Caddy’s native JSON configuration format. This allows you to write configs in a more readable format while maintaining the full power of Caddy’s JSON structure.The Caddyfile is parsed into tokens and then adapted to JSON. Environment variables in
{$ENVIRONMENT_VARIABLE} notation are replaced before parsing begins.Structure
A Caddyfile consists of server blocks, which define how Caddy should handle requests for specific sites or addresses.Server Blocks
Fromcaddyconfig/caddyfile/parse.go:753-761:
- Starts with one or more addresses (site addresses or labels)
- Contains directives that configure behavior
- Can optionally use curly braces
{}for multiple directives
Syntax Rules
Tokens and Whitespace
From the lexer implementation (caddyconfig/caddyfile/lexer.go:27-37):
- Tokens are separated by whitespace
- Quoted strings can contain whitespace:
"hello world" - Backtick strings preserve literal content:
`raw text` - Comments start with
#and continue to end of line
Quotes and Escaping
- Double Quotes
- Backticks
- Heredoc
Environment Variables
Environment variables are replaced during parsing (caddyconfig/caddyfile/parse.go:67-111):
Directives
Directives are keywords that configure Caddy’s behavior. Fromcaddyconfig/caddyfile/parse.go:783-795:
- Appear on a single line:
respond "OK" - Open a block with arguments:
reverse_proxy localhost:8080 { ... } - Span multiple lines with line continuation
\
Advanced Features
Snippets
Reusable configuration blocks defined with parentheses (caddyconfig/caddyfile/parse.go:710-717):
Named Routes
Define reusable route handlers with&(name) syntax:
Import Directive
Import configurations from other files (caddyconfig/caddyfile/parse.go:356-579):
Matchers
Request matchers filter which requests a directive applies to:Matchers must be defined within a site block, not globally. Attempting to define matchers globally (e.g.,
@matcher ...) will result in an error.Parsing Process
The Caddyfile parsing follows these steps (caddyconfig/caddyfile/parse.go:30-58):
- Tokenization - Input is lexed into tokens
- Environment Variable Expansion -
{$VAR}patterns are replaced - Token Grouping - Tokens are grouped by server blocks
- Directive Parsing - Each directive is parsed into segments
- Import Processing - Import directives are resolved and inlined
- Adaptation - The structured data is converted to JSON
Common Patterns
HTTPS and TLS
Multiple Routes
Health Checks and Logging
Best Practices
Formatting: Use
caddy fmt to automatically format your Caddyfile. The adapter checks formatting and warns if input differs from formatted output.- Use snippets for common configuration blocks
- Organize with imports for large configurations
- Define matchers for complex routing logic
- Add comments to document your configuration
- Test configs with
caddy validatebefore deploying
Error Handling
Common parsing errors and their solutions:| Error | Cause | Solution |
|---|---|---|
unexpected token '{' | Missing space before { | Add space: directive { |
wrong argument count | Missing required arguments | Check directive documentation |
unexpected EOF | Unclosed block | Add closing } |
import pattern not found | Invalid import path | Verify file exists |
mismatched heredoc marker | Incorrect closing marker | Match opening marker exactly |
See Also
- JSON Configuration - Caddy’s native JSON format
- Configuration Structure - Overall config organization
- HTTP Handlers - Available HTTP handlers