Lesson 3 of 9 · 20m
JSON Config and the Admin API: What the Caddyfile Adapts To
Caddyfile is an adapter over JSON; understanding `caddy adapt`, `/config/`, and safe reloads prevents accidental control-plane mistakes.
Caddy has two configuration surfaces, and mixing them casually creates confusion. The Caddyfile is the ergonomic human format. JSON is the actual configuration model Caddy runs. Once you understand that relationship, features like config inspection, API-driven automation, and safe reloads stop feeling mysterious.
Adapt the file before you debug the server
caddy adapt --config /etc/caddy/Caddyfile --prettyThis prints the JSON your Caddyfile becomes. When behavior does not match expectations, compare this output before rewriting the site block. In particular, adaptation reveals how matchers, routes, TLS automation policies, and directive ordering were interpreted.
Think of it like compiling:
Caddyfile --adapt--> JSON config --load/reload--> running serverThe Caddyfile is not a scripting language executing inside the server. It is input to an adapter.
The admin endpoint is a control plane
By default, Caddy exposes the admin API on localhost:2019. Useful endpoints include:
curl http://localhost:2019/config/ | jq .
curl http://localhost:2019/config/apps/http/servers | jq .Because it can replace the entire running configuration, treat the admin endpoint as privileged machinery. Binding it to a public interface without authentication is not a convenience; it is remote infrastructure control. If you expose it inside a private network, still restrict it with firewall rules and authentication.
Reloads are transactional, not magical
caddy reload --config /etc/caddy/CaddyfileA reload adapts and validates the new config, then swaps it into the running process. If validation fails, the old config keeps running. That is a strong operational property — but it does not validate whether your upstream application is healthy, whether DNS points here, or whether certificates can actually be issued.
For scripted changes, the API can also load JSON directly:
curl -X POST http://localhost:2019/load -H 'Content-Type: application/json' -d @caddy.jsonDo not alternate between API-managed JSON and hand-edited Caddyfiles without deciding which one is authoritative. Caddy will happily run either; your team will not happily debug both.
When JSON is the better interface
Use JSON when another system owns the desired state: container orchestration, tenant provisioning, dynamic routing generation, or integration tests. Use Caddyfile when humans own the file. Some mature systems use both layers, but with one direction of authorship: generate Caddyfile or JSON, never hand-edit the generated artifact.
Field notes
caddy adaptis read-only; it does not apply configuration.caddy reloadneeds the same adapter context as the original config. A Caddyfile reload is not equivalent to posting arbitrary JSON.- The admin API's default localhost binding is a safety feature. Do not "fix" it during troubleshooting.
- Record the rendered config in config management. During incidents, the running JSON matters more than what someone remembers writing.
Was this lesson helpful?