Skip to main content
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.

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

From caddyconfig/caddyfile/parse.go:753-761:
Each server block:
  • 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

Heredoc markers must contain only alphanumeric characters, dashes, and underscores. The closing marker must match the opening marker exactly.

Environment Variables

Environment variables are replaced during parsing (caddyconfig/caddyfile/parse.go:67-111):

Directives

Directives are keywords that configure Caddy’s behavior. From caddyconfig/caddyfile/parse.go:783-795:
Directives can:
  • 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):
Glob patterns in imports may only contain one wildcard (*) to prevent performance issues. Imported files starting with . are automatically skipped.

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):
  1. Tokenization - Input is lexed into tokens
  2. Environment Variable Expansion - {$VAR} patterns are replaced
  3. Token Grouping - Tokens are grouped by server blocks
  4. Directive Parsing - Each directive is parsed into segments
  5. Import Processing - Import directives are resolved and inlined
  6. 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.
  1. Use snippets for common configuration blocks
  2. Organize with imports for large configurations
  3. Define matchers for complex routing logic
  4. Add comments to document your configuration
  5. Test configs with caddy validate before deploying

Error Handling

Common parsing errors and their solutions:

See Also