Skip to content

How to Fix Caddy Caddyfile Syntax Error

DodaTech Updated 2026-06-24 2 min read

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 fmt after every Caddyfile edit
  • Use caddy validate before restarting
  • Keep one site per Caddyfile block
  • Use snippets ((name)) for reusable configuration blocks

Common Mistakes with caddyfile error

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### Why does Caddy say "unknown directive" for a valid directive?

The directive might be from a plugin that is not installed, or the syntax is for Caddy v1 (which uses a different format). Caddy v2 changed many directive names. For example, proxy in v1 became reverse_proxy in v2, and tls config moved to global options.

What is the correct order for Caddyfile directives?

Caddy processes directives in a specific order: root, header, rewrite, route, reverse_proxy, file_server, respond. Use caddy validate to check ordering. If you need precise control, use the route block to define your own processing order.

How do I include multiple sites in one Caddyfile?

Simply add multiple site blocks: site1.com { ... } followed by site2.com { ... }. Each block starts with the site address. Use snippets with (snippet_name) for shared configuration between sites, referenced with import snippet_name.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro