Skip to content

Caddyfile vs JSON config — which to use and when

The trade-offs between the Caddyfile DSL and Caddy native JSON config — what each is for, where each falls short, and the operational pattern an MSP picks for production.

Caddy accepts two config formats: the Caddyfile (a DSL designed to be readable by humans) and JSON (the native config that the Caddyfile compiles down to). The Caddyfile is what almost every tutorial shows; JSON is what Caddy actually runs. This article is the comparison we make with clients when picking which format to standardize on — the trade-offs, the operational implications, and the hybrid pattern that works for most production fleets.

How to verify

Whichever you are running, you can see both forms with these commands:

caddy adapt --config /etc/caddy/Caddyfile --pretty | head -50
curl -s http://localhost:2019/config/ | jq . | head -50
caddy validate --config /etc/caddy/Caddyfile
caddy fmt --overwrite /etc/caddy/Caddyfile

caddy adapt translates Caddyfile to JSON without applying it — useful for diffing. The /config/ admin endpoint returns the currently loaded JSON, which is the source of truth no matter how it was loaded.

What’s happening

The Caddyfile is a config DSL with a small grammar: global options, site blocks, directives, and matchers. It is line-oriented, supports { and } for nesting, and uses the same indentation conventions as Apache or Nginx configs. Internally, when Caddy boots with a Caddyfile, an “adapter” parses it and emits the equivalent JSON config; that JSON is what Caddy actually runs.

JSON is the native config. It is verbose, hierarchical, and maps directly to Caddy’s internal module tree. Every directive in the Caddyfile maps to a specific path in the JSON — reverse_proxy localhost:3000 becomes:

{
  "handler": "reverse_proxy",
  "upstreams": [{ "dial": "localhost:3000" }]
}

You read the Caddyfile faster; you can express everything in JSON without a translation step. The Caddyfile is a strict subset — there are a handful of advanced JSON features that the Caddyfile cannot express, the most common being arbitrary handler ordering and embedded matchers in some module configurations.

The admin API runs against JSON. POST /load accepts a JSON body and replaces the running config. GET /config/{path} returns a fragment. PUT/PATCH/DELETE apply granular edits. This is how dynamic configs work — a service mesh controller writes JSON fragments without ever touching the Caddyfile.

Decision

The trade-off:

AspectCaddyfileJSON
Human readabilityHigh — purpose-built DSLLow — verbose, deeply nested
ExpressivenessCovers ~95% of featuresCovers 100%
TemplatingSnippets + imports onlyAny tool can emit JSON
Diff-ability in CRExcellentTolerable with jq
API-driven configNo — file-basedYes — admin API native
Hot reloadsystemctl reload caddyPOST /load or PATCH
Order of directivesCaddy auto-ordersYou control
Source of truthThe Caddyfile on diskThe /config/ endpoint

When to pick Caddyfile. Most production fleets. Static config, file-driven, version controlled. The Caddyfile is editable by hand, reviewable in a pull request, and the caddy fmt command keeps formatting consistent. Use snippets and imports (caddy snippets and imports) to factor out repetition. Reload via systemd.

When to pick JSON. Dynamic, API-driven setups. Container orchestrators that need to add and remove backends on the fly. Multi-tenant platforms that issue per-tenant routes from a control plane. Any case where the config is generated by code rather than edited by people.

The hybrid. Most of our deployments end up here. The site config (TLS, routes, common matchers) lives in a Caddyfile. The dynamic bits (backend pool membership, per-tenant overrides) live in JSON applied via the admin API. The Caddyfile boots first; the JSON is layered on top via POST /config/apps/http/servers/srv0/routes.

Examples — same config, both forms

The Caddyfile:

{
    email ops@example.com
    admin :2019
}

example.com {
    encode zstd gzip
    reverse_proxy localhost:3000 {
        health_uri /health
        health_interval 10s
    }
    log {
        format json
    }
}

The equivalent JSON (truncated for readability, full output via caddy adapt):

{
  "admin": { "listen": ":2019" },
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [":443"],
          "routes": [{
            "match": [{ "host": ["example.com"] }],
            "handle": [
              { "handler": "encode", "encodings": { "zstd": {}, "gzip": {} } },
              { "handler": "reverse_proxy",
                "upstreams": [{ "dial": "localhost:3000" }],
                "health_checks": {
                  "active": { "uri": "/health", "interval": "10s" }
                }
              }
            ],
            "terminal": true
          }],
          "logs": { "default_logger_name": "log0" }
        }
      }
    }
  },
  "logging": {
    "logs": {
      "log0": { "encoder": { "format": "json" } }
    }
  }
}

Same behavior. The Caddyfile is 12 lines, the JSON is 30+. The JSON exposes every internal knob; the Caddyfile picks sane defaults for everything you didn’t say.

Operational notes

  • The caddy fmt command rewrites the Caddyfile to canonical formatting. Run it before committing — Caddy parses inconsistent indentation but reviewers prefer consistency.
  • The admin API is enabled by default on 127.0.0.1:2019 only. If you do not use it, leave it alone — but never expose it to a public interface. A POST /load against an exposed admin port replaces the entire running config.
  • caddy adapt --config /etc/caddy/Caddyfile --pretty > config.json once, then POST /load against the JSON — useful for one-time conversion when migrating from Caddyfile-driven to API-driven.
  • Caddy auto-orders directives in the Caddyfile to a sensible request pipeline; in JSON you control the order yourself. Mixing the two is a common source of “why is header running after file_server?” confusion.
  • The --watch flag on caddy run makes the binary watch the Caddyfile for changes and reload automatically. Useful in dev, not in prod — production uses systemctl reload caddy so the change is auditable.

Stack Harbor standardizes on the hybrid pattern for clients running Caddy at any meaningful scale — a Caddyfile under git for the stable bits, a controller that PATCHes the admin API for the dynamic bits, and the storage backing the cert state shared across both. If you have a Caddy fleet that needs to be both file-driven (for review) and API-driven (for orchestration), this is the kind of work covered by our Managed Operations practice.