Seeking Advice: Configuring Caddy and auto reload from Spring Boot Backend

Hello Caddy community,

I’m working on a project where I need to dynamically add domains to Caddy’s reverse_proxy block from my Spring Boot backend. Specifically, I want to be able to use an endpoint in my Spring Boot app, let’s call it /addDomain, to add new domains to Caddy without manually editing the Caddyfile.

I’ve been exploring options like using Caddy’s REST API or constructing a JSON configuration, but I’m not sure of the best approach or the exact steps involved. Could someone please provide guidance or share their experience if they’ve done something similar before?

My goal is to achieve a setup like this in the Caddyfile:

caddyfile

domain1.com, comingdoming.com, other-coming-domain.com {
    reverse_proxy localhost:8080
}

I want to be able to programmatically add domains to this block using my Spring Boot backend without having to edit the Caddyfile directly.

Here’s an example JSON configuration I’ve been considering:

json

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "routes": [
            {
              "match": {
                "host": ["domain1.com", "comingdoming.com", "other-coming-domain.com"]
              },
              "handle": [
                {
                  "handler": "reverse_proxy",
                  "upstreams": ["localhost:8080"]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

Would this JSON configuration be valid for achieving the reverse proxy setup I described above? Any help or pointers in the right direction would be greatly appreciated. Thanks in advance!

The best way to learn to write a JSON config is by using the caddy adapt -p command which adapts your Caddyfile to JSON. You can use that to see how changes to a Caddyfile affects the JSON.

What is your goal here exactly, though? Is this to allow customers to point their custom domains to your server? If so, you should use Caddy’s On-Demand TLS feature instead, which is meant to solve that usecase. It would mean that no config changes are necessary to serve additional domains.