caddy
$ caddy
Caddy is an extensible server platform written in Go.
At its core, Caddy merely manages configuration. Modules are plugged
in statically at compile-time to provide useful functionality. Caddy's
standard distribution includes common modules to serve HTTP, TLS,
and PKI applications, including the automation of certificates.
To run Caddy, use:
- 'caddy run' to run Caddy in the foreground (recommended).
- 'caddy start' to start Caddy in the background; only do this
if you will be keeping the terminal window open until you run
'caddy stop' to close the server.
...
alexlai@orgpi516G ~/build/caddy-getting-started $ caddy run
2023/12/16 01:46:26.144 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/12/16 01:46:26.144 INFO serving initial configuration
alexlai@orgpi516G ~/build/caddy-getting-started $ curl localhost:2019/config/
null
- Give Caddy a config, using json file format
- caddy.json
{
"apps": {
"http": {
"servers": {
"example": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}
]
}
}
}
}
}
- then, upload it
curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @caddy.json
- read it back,
$ curl localhost:2019/config/
{"apps":{"http":{"servers":{"example":{"listen":[":2015"],"routes":[{"handle":[{"body":"Hello, world!","handler":"static_response"}]}]}}}}}
- test
$ curl localhost:2015
Hello, world!
- config file using Caddyfile
The above caddy.json, could be expressed in Caddyfile as,
:2015
respond "Hello, world! From YuShei!"
- ctl-c and restart
alexlai@orgpi516G ~/build/caddy-getting-started $ caddy run
2023/12/16 03:11:00.503 INFO using adjacent Caddyfile
2023/12/16 03:11:00.508 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/12/16 03:11:00.508 INFO http.log server running {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/12/16 03:11:00.508 INFO tls.cache.maintenance started background certificate maintenance {"cache": "0x40001dc900"}
2023/12/16 03:11:00.508 INFO autosaved config (load with --resume flag) {"file": "/home/alexlai/.config/caddy/autosave.json"}
2023/12/16 03:11:00.509 INFO serving initial configuration
2023/12/16 03:11:00.509 INFO tls cleaning storage unit {"description": "FileStorage:/home/alexlai/.local/share/caddy"}
2023/12/16 03:11:00.510 INFO tls finished cleaning storage units
...
- check
$ curl localhost:2015
Hello, world! From Yushei!
- Define Multiple Sites, as multiple.json
{
"apps": {
"http": {
"servers": {
"hello": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}
]
},
"bye": {
"listen": [":2016"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Goodbye, world!"
}]
}
]
}
}
}
}
}
- load it
curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @multiple.json
- test it
alexlai@orgpi516G ~/build/caddy-getting-started $ curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @multiple.json
alexlai@orgpi516G ~/build/caddy-getting-started $ curl localhost:2015
Hello, world!
alexlai@orgpi516G ~/build/caddy-getting-started $ curl localhost:2016
Goodbye, world!