Proxy: How to try next upstream on any error code

I’m trying to set up a proxy with two upstreams, and I want caddy to try the next upstream if the first one it tried answered with a non 2XX status code

Essentially I don’t know which server will be able to fulfill a given request ahead of time so I want to try both and return whichever is successful.

Here’s what I have so far:

example.com {
    proxy / upstreamone upstreamtwo {
        try_duration 5s
    }
}

This only gives me one of the upstreams at random and lets errors pass through. I’d like the behaviour of status_check but on every request. Is this possible?

Caddy’s proxy middleware handles errors only for hosts that fail to return a response (i.e. when a net.Dial() call fails or if the body the backend server returns is too large). This is a known issue, see https://github.com/mholt/caddy/issues/753.

The long-term solution is the rewrite of the proxy middleware, which is planned: https://github.com/mholt/caddy/issues/1639

1 Like

Theoretically you could health_check every second, but only on the same path each time, there’s no way to make it do health checks per request for that request path.

Having a proxy “fork” the request to two upstreams simultaneously, and return content from whichever one returns a 2xx result first, is pretty advanced logic and beyond the current scope of proxy and the Caddyfile.

I’m not even sure if you could do that with something like HAProxy.

I didn’t necessarily mean request both upstreams at once, one first and then the other if the first one fails would be good enough for me.

It’s cool though I suppose I’ll wait for the rewrite.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.