首页 > 解决方案 > Caddy:如何通过 API 添加多个反向代理而不使用 Caddyfile?

问题描述

我可以使用这样的 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

解决方案


不是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


推荐阅读