caddy: lb_retries apparently not working
I’d like to be able to recreate/restart Docker services that are hooked into Caddy via reverse_proxy without downtime. Usually a (POST) request takes about 10 seconds to complete. My config is super simple:
example.com {
log {
format console
output stdout
}
root * /srv
file_server {
precompressed br gzip
}
reverse_proxy /-/proxy1 http://docker_container1:9292 {
lb_retries 3
lb_try_interval 1s
}
reverse_proxy /-/proxy2 http://docker_container2:9292 {
lb_retries 3
lb_try_interval 1s
}
}
When running a bunch of requests and restarting the container before the responses were returned, I always get 502 errors, despite the lb_retries directive.
The error messages look like this:
2024/04/21 12:35:27.404 ERROR http.log.access.log0 handled request {"request": {"remote_ip": "62.153.218.40", "remote_port": "44362", "client_ip": "62.153.218.40", "proto": "HTTP/1.1", "method": "POST", "host": "example.com", "uri": "/-/proxy1", "headers": {"Accept-Encoding": ["gzip, compress, deflate, br"], "Connection": ["keep-alive"], "Accept": ["application/json, text/plain, */*"], "Content-Type": ["application/json"], "User-Agent": ["axios/1.6.7"], "Content-Length": ["67993"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "", "server_name": "example.com"}}, "bytes_read": 0, "user_id": "", "duration": 1.014601761, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"], "Alt-Svc": ["h3=\":443\"; ma=2592000"]}}
{"level":"error","ts":1713702927.4045582,"logger":"http.log.error.log0","msg":"readfrom tcp 172.18.0.2:34872->172.18.0.3:9292: http: invalid Read on closed Body","request":{"remote_ip":"62.153.218.40","remote_port":"44362","client_ip":"62.153.218.40","proto":"HTTP/1.1","method":"POST","host":"example.com","uri":"/-/proxy1","headers":{"Accept-Encoding":["gzip, compress, deflate, br"],"Connection":["keep-alive"],"Accept":["application/json, text/plain, */*"],"Content-Type":["application/json"],"User-Agent":["axios/1.6.7"],"Content-Length":["67993"]},"tls":{"resumed":true,"version":772,"cipher_suite":4865,"proto":"","server_name":"example.com"}},"duration":1.014601761,"status":502,"err_id":"cr390dupf","err_trace":"reverseproxy.statusError (reverseproxy.go:1267)"}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 32 (16 by maintainers)
@gottlike No need to wait, please feel free to download our pre-compiled binaries from CI artifacts: https://github.com/caddyserver/caddy/actions/runs/8808836194#artifacts 😁
Edit: Lightning Mohammed beat me to it again!
Thanks guys! With master it works 🥳
You can grab the CI artifact from here
https://github.com/caddyserver/caddy/actions/runs/8808836194#artifacts
@gottlike Oh, I am using master, good point.
We’re hoping to tag 2.8 (beta) this week, can you try master? That should work!
Damn, and I omitted the warning so nobody would notice 😋
How can we debug this further? I edited my post and added the Caddy logs, maybe that helps to shed a light?
Perhaps 413 Content Too Large?
Yeah, connection failures (before the HTTP request is written upstream) is always allowed to retry, because we know it’s always safe. The
lb_retry_matchis for enabling retries other than justGETif it fails after connecting.Okay, so the “always retry, no matter what
lb_retry_matchsays”-behaviour only happens if Caddy was never able to initially connect to the upstream? That would be safe then, I guess. I figured it would also do that if it did connect initially and the upstream connection broke while processing the request. If that’s not the case and thelb_retry_matchis taken into account in those cases, that’s fine.Yeah, exactly, you’re running Caddy behind Caddy, so of course it’ll have two copies of the
Server: Caddyheader. That’s working as intended. Each server adds their ownServerheader.So you want to not retry even connection problems? But why? I can’t think of any reason why that would be helpful.
The logic for retries is here:
https://github.com/caddyserver/caddy/blob/6a0299905479083f250c39d5780c305f82863273/modules/caddyhttp/reverseproxy/reverseproxy.go#L1013-L1071
Actually, just following up with d93e027e01905406671ed8df295d5e48d16645c2 which also preserves the body in case one of the backends partially (or totally) depletes it before having the connection severed.
I got the email. Thank you for reaching out!
While investigating this, I realized this should already work:
I start the proxy, and then I fire off a request:
and then quickly, before the load balancer gives up, I bring up the simulated backend:
But I was getting
readFromerrors, because the request body was being closed… but it was definitely being fully read into the buffer! So closing the request body shouldn’t matter.Anyway, all I had to do was patch half a line of code: 613d544a4775b831595f223028fee76b980aa003
Seems to work now.
See https://caddyserver.com/sponsor, you can reach out that way to get a quote.