caddy
2025/10/31大约 1 分钟
caddy
启动,停止,运行
caddy run   # 前台运行 Ctrl+C 停止caddy
caddy start # 后台运行
caddy stop  # 后台停止 或者使用API的/stop接口停止服务
caddy reload # 重载配置 不停机加载/更改配置运行 Demo
JSON 配置
caddy --help
caddy run
## 加载配置
curl localhost:2019/load \
	-X POST \
	-H "Content-Type: application/json" \
	-d '{"apps":{"http":{"servers":{"example":{"listen":[":2015"],"routes":[{"handle":[{"handler":"static_response","body":"Hello, world!"}]}]}}}}}'
# 管理API查看配置
curl localhost:2019/config/
# 使用curl测试它是否符合预期
curl localhost:2015
Hello, world!Caddyfile 配置
:2015
respond "Hello, world!"现在有一个Caddyfile在当前文件夹,运行caddy run
caddy run
caddy run --config /path/to/Caddyfile
# 配置适配器来将我们的Caddyfile转换成Caddy的原生JSON结构
caddy adapt # 当前目录配置文件 Caddyfile
caddy adapt --config /path/to/Caddyfile静态文件
命令行
caddy file-server # 当前目录 80端口
caddy file-server --listen :2015 # 指定端口
caddy file-server --browse       # 列目录
caddy file-server --root ~/mysite # 指定目录Caddyfile - caddy run
localhost:2015
file_server browse
root * /home/me/mysite反向代理
命令行
caddy reverse-proxy --to 127.0.0.1:9000
caddy reverse-proxy --from :2016 --to 127.0.0.1:9000Caddyfile - caddy run
:2016
reverse_proxy 127.0.0.1:9000HTTPS快速入门
Caddyfile - caddy run
example.com
respond "Hello, privacy!"file-server命令
caddy file-server --domain example.comreverse-proxy命令
caddy reverse-proxy --from example.com --to localhost:9000JSON配置
{
	"apps": {
		"http": {
			"servers": {
				"hello": {
					"listen": [":443"],
					"routes": [
						{
							"match": [{
								"host": ["example.com"]
							}],
							"handle": [{
								"handler": "static_response",
								"body": "Hello, privacy!"
							}]
						}
					]
				}
			}
		}
	}
}实验室
代理到指定域名,并修改host请求头为指定域名
# http
caddy reverse-proxy --from :2016 --to cip.cc:80 --change-host-header
# https
caddy file-server --domain test.us.seccmd.net
caddy reverse-proxy --from test.us.seccmd.net --to cip.cc:80 --change-host-header
# caddy run --config Caddyfile
test.us.seccmd.net {
  reverse_proxy https://github.com:443 {
    header_up Host github.com
  }
}