我可以使用这样的 Caddyfile(+ https withletsencrypt)拥有一个具有多个域的服务器:
site1.com {
reverse_proxy localhost:3001
}
site2.com {
reverse_proxy localhost:3002
}
site3.com {
reverse_proxy localhost:3003
}
但是我想在没有 Caddyfile 的情况下运行 caddy 并且我想通过管理 API 动态添加这样的代理。他们有一个 API为此,但我无法理解如何在不需要 Caddyfile 的情况下为 caddy 添加新的反向代理。
我正在寻找此请求的 JSON 格式,它可以是这样的(伪请求):
curl localhost:2019/load \
-X POST \
-H "Content-Type: application/json" \
-data "{'domain': 'site1.com', 'tls': 'yes', 'proxy': 'http://localhost:3001'}"
curl localhost:2019/load \
-X POST \
-H "Content-Type: application/json" \
-data "{'domain': 'site2.com', 'tls': 'yes', 'proxy': 'http://localhost:3002'}"
是否可以仅通过 API 添加反向代理?我正在使用 Caddy v2.0
最佳答案
不是caddy adapt
你需要什么?
> caddy adapt --config /path/to/Caddyfile
{"apps":{"http":{"servers":{"srv0":{"listen":[":443"],"routes":[{"match":[{"host":["site1.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"localhost:3001"}]}]}]}],"terminal":true},{"match":[{"host":["site2.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"localhost:3002"}]}]}]}],"terminal":true},{"match":[{"host":["site3.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"localhost:3003"}]}]}]}],"terminal":true}]}}}}}
https://caddyserver.com/docs/getting-started
关于Caddy:如何通过 API 添加多个反向代理而不使用 Caddyfile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62556632/