ยง2023-06-02
Step 1 โ Building Caddy using xcaddy
$ mkdir caddy && cd $_
$ xcaddy build
$ ls -l
total 38928
-rwxr-xr-x 1 alexlai alexlai 39862272 Jun 2 07:46 caddy
$ ./caddy version
v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=
caddy start
$ ./caddy start
2023/06/02 00:06:12.152 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/06/02 00:06:12.152 INFO serving initial configuration
Successfully started Caddy (pid=4860) - Caddy is running in the background
$ curl localhost:2019
2023/06/02 00:06:29.999 INFO admin.api received request {"method": "GET", "host": "localhost:2019", "uri": "/", "remote_ip": "127.0.0.1", "remote_port": "52718", "headers": {"Accept":["*/*"],"User-Agent":["curl/8.1.2"]}}
404 page not found
$ ps aux|grep caddy
alexlai 4860 0.2 0.3 751464 39168 pts/1 Sl 08:06 0:00 ./caddy run --pingback 127.0.0.1:35627
alexlai 4879 0.0 0.0 7984 2432 pts/1 S+ 08:06 0:00 grep --color=auto caddy
$ kill -9 4860
- caddy run
$ ./caddy run
2023/06/02 00:08:39.234 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/06/02 00:08:39.235 INFO serving initial configuration
2023/06/02 00:08:48.476 INFO admin.api received request {"method": "GET", "host": "localhost:2019", "uri": "/", "remote_ip": "127.0.0.1", "remote_port": "35416", "headers": {"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.5"],"Connection":["keep-alive"],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0"]}}
2023/06/02 00:08:48.557 INFO admin.api received request {"method": "GET", "host": "localhost:2019", "uri": "/favicon.ico", "remote_ip": "127.0.0.1", "remote_port": "35416", "headers": {"Accept":["image/avif,image/webp,*/*"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.5"],"Connection":["keep-alive"],"Referer":["http://localhost:2019/"],"Sec-Fetch-Dest":["image"],"Sec-Fetch-Mode":["no-cors"],"Sec-Fetch-Site":["same-origin"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0"]}}
^C2023/06/02 00:09:01.170 INFO shutting down {"signal": "SIGINT"}
2023/06/02 00:09:01.170 WARN exiting; byeee!! ๐ {"signal": "SIGINT"}
2023/06/02 00:09:01.170 INFO admin stopped previous server {"address": "localhost:2019"}
2023/06/02 00:09:01.170 INFO shutdown complete {"signal": "SIGINT", "exit_code": 0}
- caddy.json as
{
"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
$ curl localhost:2019/config/
{"apps":{"http":{"servers":{"example":{"listen":[":2015"],"routes":[{"handle":[{"body":"Hello, world!","handler":"static_response"}]}]}}}}}
$ curl localhost:2019/
404 page not found
$ curl localhost:2015/
Hello, world!
-
Another way to configure Caddy is with the Caddyfile. The same config we wrote in JSON above can be expressed simply as:
-
Caddyfile
:2015
respond "Hello, world!"