ยง2023-07-01
But most people will use JSON+API or Caddyfile+CLI combinations.
Another way to configure Caddy is with the Caddyfile. The same config we wrote in JSON as in caddy.json
can be expressed simply as:
- Caddyfile
:2015
respond "Hello, world!"
- Run
$ caddy run
2023/07/01 00:18:32.116 INFO using adjacent Caddyfile
2023/07/01 00:18:32.126 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/07/01 00:18:32.126 INFO tls.cache.maintenance started background certificate maintenance {"cache": "0xc0006e8380"}
2023/07/01 00:18:32.126 INFO http.log server running {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/07/01 00:18:32.127 INFO tls cleaning storage unit {"description": "FileStorage:/home/alexlai/.local/share/caddy"}
2023/07/01 00:18:32.127 INFO tls finished cleaning storage units
2023/07/01 00:18:32.127 INFO autosaved config (load with --resume flag) {"file": "/home/alexlai/.local/share/caddy/autosave.json"}
2023/07/01 00:18:32.127 INFO serving initial configuration
- test
$ curl localhost:2015
Hello, world!
if, Caddyfile
:2015
respond "Hello, world!"
<-- blank
<-- blank
$ caddy run
2023/07/01 00:26:58.081 INFO using adjacent Caddyfile
2023/07/01 00:26:58.082 WARN Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies {"adapter": "caddyfile", "file": "Caddyfile", "line": 4}
2023/07/01 00:26:58.088 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/07/01 00:26:58.089 INFO tls.cache.maintenance started background certificate maintenance {"cache": "0xc0004b33b0"}
2023/07/01 00:26:58.089 INFO tls cleaning storage unit {"description": "FileStorage:/home/alexlai/.local/share/caddy"}
2023/07/01 00:26:58.089 INFO tls finished cleaning storage units
2023/07/01 00:26:58.089 INFO http.log server running {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/07/01 00:26:58.089 INFO autosaved config (load with --resume flag) {"file": "/home/alexlai/.local/share/caddy/autosave.json"}
2023/07/01 00:26:58.090 INFO serving initial configuration
$ caddy fmt --overwrite
$ caddy run
2023/07/01 00:27:54.708 INFO using adjacent Caddyfile
2023/07/01 00:27:54.714 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/07/01 00:27:54.715 INFO tls.cache.maintenance started background certificate maintenance {"cache": "0xc00059d3b0"}
2023/07/01 00:27:54.715 INFO tls cleaning storage unit {"description": "FileStorage:/home/alexlai/.local/share/caddy"}
2023/07/01 00:27:54.715 INFO tls finished cleaning storage units
2023/07/01 00:27:54.715 INFO http.log server running {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/07/01 00:27:54.715 INFO autosaved config (load with --resume flag) {"file": "/home/alexlai/.local/share/caddy/autosave.json"}
2023/07/01 00:27:54.715 INFO serving initial configuration
use
caddy adapt
to check Caddyfile