1. The problem I’m having:
I have a simple Caddyfile that reverse-proxies two different servers:
example {
reverse_proxy localhost:3000
}
api.example {
reverse_proxy localhost:4000
}
www.example {
# strip "www" prefix
redir https://example{uri} permanent
}
Now, I’d like to be able to update the upstream for the frontend server
(on example
) from localhost:3000
to localhost:3001
. Of course,
I can rewrite the whole config and reload it, but I’d like to use
Caddy’s object ID (@id
) functionality to patch it in-place.
As written, I can issue a PATCH update to a long path under the admin server:
localhost:2019/config/apps/http/servers/srv0/routes/2/handle/0/routes/0/handle/0/upstreams/0/dial
but not only is this unwieldy, it also contains indices that are not
obvious from the Caddyfile: specifically, the index in routes/2/
.
Ideally, I would like to add a bit of syntax to my Caddyfile like this:
example {
# new!
# +----------------+
reverse_proxy $frontend-upstream localhost:3000
}
api.example {
reverse_proxy localhost:4000
}
www.example {
# strip "www" prefix
redir https://example{uri} permanent
}
…which would adapt to JSON like this:
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [":443"],
"routes": [
{"match": [{"host": ["api.example"]}], "handle": [/* ... */]}, "terminal": true},
{"match": [{"host": ["www.example"]}], "handle": [/* ... */]}, "terminal": true},
{
"match": [{"host": ["example"]}],
"handle": [{
"handler": "subroute",
"routes": [{
"handle": [{
"handler": "reverse_proxy",
"upstreams": [{
"@id": "frontend-upstream", // <-- new!
"dial": "localhost:3000"
}]
}]
}]
}],
"terminal": true
}
]
}
}
}
}
}
Then, I could PATCH localhost:2019/id/frontend-upstream/dial
with the
new string "localhost:3001"
.
It’s okay with me if the @id
doesn’t attach at the leafmost node, as
long as I can unambiguously navigate down from there to the actual
upstream definition.
Unfortunately, I wasn’t able to find documentation on how to write this
in the Caddyfile. I see docs about @id
and about Caddyfiles but not
about using them together. I tried @myid
, $myid
, [myid]
, etc. in
various positions, but those variously were parse errors or resolved to
the wrong config.
2. Error messages and/or full log output:
Shooting in the dark, with this Caddyfile:
example {
reverse_proxy @frontend-upstream localhost:3000
}
api.example {
reverse_proxy localhost:4000
}
www.example {
# strip "www" prefix
redir https://example{uri} permanent
}
…caddy adapt
emits this error:
Error: parsing caddyfile tokens for 'reverse_proxy': unrecognized matcher name: @frontend-upstream
…which of course makes sense, since this is the syntax for matchers, not
@id
s. I don’t know what the right syntax is.
3. Caddy version:
v2.7.5 h1:HoysvZkLcN2xJExEepaFHK92Qgs7xAiCFydN5x5Hs6Q=
4. How I installed and ran Caddy:
Installed per the Debian, Ubuntu, Raspbian docs, using the
Cloudsmith stable repos.
a. System environment:
Ubuntu 22.04.3, Linux 6.2.0, x86-64
b. Command:
caddy adapt
c. Service/unit/compose file:
Not relevant.
d. My complete Caddy config:
example {
# how can I attach an "@id" to this upstream?
# +------------+
reverse_proxy localhost:3000
}
api.example {
reverse_proxy localhost:4000
}
www.example {
# strip "www" prefix
redir https://example{uri} permanent
}