Support load balancing across multi reverse_proxy
Summary
Caddy currently applies one transport configuration across all upstreams when using a single reverse_proxy directive. This request proposes the ability to load balance across several individual reverse_proxy directives that share the same upstream but use distinct transport settings (e.g., different forward_proxy_url values).
Description
In some scenarios, users need to direct requests to the same backend (http://myapp) via multiple forward proxies. The desired configuration is to have multiple reverse_proxy blocks—each configured with a unique transport (and corresponding forward_proxy_url)—that are load balanced as a single logical backend. For example, the intended pseudocode is:
# Pseudocode: Desired behavior (currently not supported)
load_balancer {
reverse_proxy http://myapp {
transport http {
forward_proxy_url http://proxy-1
}
}
reverse_proxy http://myapp {
transport http {
forward_proxy_url http://proxy-2
}
}
}
Instead, Caddy currently requires that when specifying multiple upstreams in a single reverse_proxy directive, the same transport configuration applies to all. This limitation prevents a clean setup where different forward proxy URLs can be used for the same backend.
I'm interested in working on this.
My plan is to implement per-upstream transports by adding a Transport field to the Upstream struct in the Go code. The logic would be: check upstream's transport first; if nil, fallback to the handler's transport.
My main concern is the Caddyfile syntax. Would you be open to a syntax where upstreams can be defined as blocks to override the transport?
Something like this:
reverse_proxy {
# Standard upstream (uses handler transport)
to 10.0.0.1:8080
# Upstream with specific transport
to https://example.com {
transport http {
forward_proxy_url http://proxy-1
}
}
}
@mholt @francislavoie What do you think about this approach
I don't like either the idea of load_balancer wrapping proxies, nor having per-upstream config (completely incompatible with our current config layout).
I think would would be more sensible is making forward_proxy modular such that the decision of which forward proxy to use is delegated to a plugin. So then you could do like:
reverse_proxy http://myapp {
transport http {
forward_proxy round_robin http://proxy-1 http://proxy-2
}
}
Where round_robin is a plugin provided by the user (wouldn't be included by default probably) to do the decision of which proxy address to use given a request.
That makes total sense, @francislavoie. Delegating the decision to a plugin via a modular forward_proxy is definitely cleaner than tightly coupling transport config to specific upstreams. It avoids breaking the current config layout. Thanks for the insight!