How to Fix Caddy Caddyfile Syntax Error
In this tutorial, you'll learn about How to Fix Caddy Caddyfile Syntax Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Caddy fails to start with Error: adapting config using caddyfile: <a href="/compiler-design/syntax-analysis/">Parsing</a> caddyfile tokens: <a href="/compiler-design/syntax-analysis/">Parsing</a> Caddyfile: read tcp or unknown directive — the Caddyfile has syntax issues or directives are in the wrong order.
The Problem
$ caddy run
2026/06/24 10:00:00 [ERROR] adapting config using caddyfile:
parsing caddyfile tokens: /etc/caddy/Caddyfile:5 - parse error:
reading server config: unrecognized directive: ProxyPass
Step-by-Step Fix
Step 1: Use correct Caddyfile syntax
# Correct Caddy v2 syntax
example.com {
root * /var/www/html
file_server
}
api.example.com {
reverse_proxy localhost:3000
}
Step 2: Check directive ordering
example.com {
# Order matters in Caddy
root * /var/www/html
# First: matchers
@static {
path *.css *.js *.png
}
header @static Cache-Control max-age=3600
# Then: handlers
file_server
}
Step 3: Validate the Caddyfile
caddy validate
Step 4: Format the Caddyfile
caddy fmt --overwrite
Step 5: Test with a minimal config
:8080 {
respond "Hello, World!"
}
Step 6: Check Caddy version compatibility
caddy version
# Caddy v2 uses a different syntax than v1
Prevention Tips
- Run
caddy fmtafter every Caddyfile edit - Use
caddy validatebefore restarting - Keep one site per Caddyfile block
- Use snippets (
(name)) for reusable configuration blocks
Common Mistakes with caddyfile error
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world CADDY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro