Repository navigation
Closed
Description
Context
When trying to use the health_request_body directive, the use of backticks for a json body does not work.
Ex :
- health_request_body {"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]} --> This works
Host: 192.168.XXX.XXX:XXXX
User-Agent: Go-http-client/1.1
Content-Length: 63
Accept-Encoding: gzip
Content-Type: application/json
Via: 1.1 Caddy
X-Forwarded-For: 192.168.XXX.XXX
X-Forwarded-Host: blablabla.xyz
X-Forwarded-Proto: http
JSON
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": []
}
- health_request_body `{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}` --> This does not works it send an empty body
Host: 192.168.XXX.XXX:XXXX
User-Agent: Go-http-client/1.1
Content-Length: 0
Accept-Encoding: gzip
Content-Type: application/json
Via: 1.1 Caddy
X-Forwarded-For: 192.168.XXX.XXX
X-Forwarded-Host: blablabla.xyz
X-Forwarded-Proto: http
No request content
Might be interesting to write that in the doc or even adapt the code to take it into account properly!
This is not working as expected here https://caddyserver.com/docs/caddyfile/concepts#tokens-and-quotes in the backticks section!
How to repoduce
- OS : debian 12
sudo apt install mitmproxy
mitmproxy --mode reverse:http://<SERVER1_URL>>:<SERVER1_PORT> -p 8080
And here is a sample Caddyfile
http://<YOUR_DESTINATION_URL>:<YOUR_DESTINATION_PORT> {
log
reverse_proxy http://<SERVER1_URL>:<SERVER1_PORT> {
lb_policy first
health_status 2xx
health_uri /
health_method POST
health_headers {
Content-Type application/json
}
health_request_body "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_blockNumber\",\"params\":[]}"
header_up Host {upstream_hostport}
}
}
http:/<SERVER1_URL>:<SERVER1_PORT> {
log
reverse_proxy http://localhost:8080 {
header_up Host {upstream_hostport}
header_down X-Acting-Upstream "primary"
}
}
Now you will see the request handled by mitmproxy, you can inspect the request content.
Metadata
Metadata
Assignees
Labels
No labels
Activity
francislavoie commentedon May 13, 2025
Did you try escaping the opening { so Caddy doesn't read it as a placeholder?
ngotchac commentedon May 13, 2025
This indeed works, I think it's more of a documentation issue, as it wasn't obvious that
health_request_bodywas using placeholders. This will be an issue for anyone trying to send JSON requests.I also think there should be a WARN log in
caddywhen trying to use unknown/invalid placeholders, such as{"json":; WDYT?mholt commentedon May 13, 2025
We could probably switch to replacing only known placeholders.
bayardar commentedon May 14, 2025
@francislavoie Yep escaping
{works but as @ngotchac said it is not clear with what is written in the doc since we have an example like that@mholt sounds like a great idea!
francislavoie commentedon May 14, 2025
It's dependent on each directive's code. Some do
ReplaceAlland some doReplaceKnown, which is the difference in this case.IslamElsayed commentedon Sep 8, 2026
I'd like to take this, if nobody else is on it.
Following @mholt's suggestion of replacing only known placeholders: in
doActiveHealthCheck(modules/caddyhttp/reverseproxy/healthchecks.go) the body usesrepl.ReplaceAllat line 434, so any{...}the global replacer doesn't recognise is blanked and a JSONhealth_request_bodygoes out empty.The same function already does it the other way three lines apart — header values use
repl.ReplaceKnown(line 458), butHoston line 454 still usesReplaceAll, which looks unintentional rather than deliberate. I'd switch both the body andHosttoReplaceKnownand add coverage.I'd leave the header name on line 452 as-is unless you'd rather it changed too — a placeholder in a header name feels like a different question from one in a value, and I'd rather not widen the diff on a guess.
Disclosure per CONTRIBUTING: I'm using AI assistance (Claude) on this. I'll only submit code I can explain line by line and that's covered by tests.
IslamElsayed commentedon Sep 8, 2026
Correcting myself on the
Hostline before I open the PR — I said I'd switch it too, and testing says I shouldn't.Go's HTTP server blanks a
Hostheader containing braces. SendingHost: {not.a.placeholder}from a plainhttp.Clientto anhttptestserver, the handler seesr.Host == "". That's stdlib behaviour, nothing to do with Caddy.So for line 454 the two options are:
ReplaceAll(today): an unknown{...}is blanked,req.Hostends up empty, and Go falls back to the URL host — the request still works.ReplaceKnown: the literal{...}survives intoreq.Host, Go sends an invalid Host, and the upstream discards it.Changing it would make that case worse, not better, and there's no way to write an end-to-end test that asserts anything useful. So the PR touches only the body on line 434, which is the reported bug.
One thing worth noting from the tests:
ReplaceAlldoesn't just break bodies with no placeholders. A body that does contain a real one is mangled too —{"token":"{env.SOME_VAR}"}comes out as"}today, because the opening{"token":"is consumed as an unrecognised placeholder before the real one is reached. So this affects anyone sending JSON, whether or not they use placeholders.