Caddy 2: JSON config to create a reverse proxy

Posted on: 2020-07-12
Software versions: Caddy 2

From the doc, it's not exactly straightforward to find what could be the right payload to pass to Caddy's API to create a reverse proxy dynamically. It's possible to use Caddy's CLI caddy adapt command to get what is the JSON payload corresponding to a Caddyfile.

Here is the result if you want to create a https reverse proxy from mydomain.com to the target targetdomain.com.

{ "apps": { "http": { "servers": { "myServer": { "listen": [ ":443" ], "routes": [ { "match": [{ "host" : ["mydomain.com"] }], "terminal": true, "handle": [ { "handler" : "reverse_proxy", "upstreams": [ { "dial" : "targetdomain.com" }], "headers": { "request": { "set" : { "Host": ["{http.reverse_proxy.upstream.host}"] } } }, "transport": { "protocol": "http", "tls": {} } } ] } ] } } } } }