Max Block
Max Block

Reputation: 1444

Caddy: How to add multiple reverse proxies via API and not using Caddyfile?

I can have a server with multiple domains using such a Caddyfile (+ https with letsencrypt):

site1.com {
  reverse_proxy localhost:3001
}

site2.com {
  reverse_proxy localhost:3002
}

site3.com {
  reverse_proxy localhost:3003
}

But I want to run caddy without Caddyfile and I want to add such proxies dynamically via admin API.

They have an API for it, but I can't understand how to add a new reverse proxy for caddy without needing a Caddyfile.

I'm looking for a JSON format of this request, it can be something like this (pseudo-request):

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'}"

Is it possible to add reverse proxies via API only? I'm using Caddy v2.0

Upvotes: 5

Views: 3203

Answers (1)

nomad
nomad

Reputation: 541

Isn't caddy adapt what you need?

> 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

Upvotes: 1

Related Questions