I’d like to proxy to varnish – I can figure that out, thanks to the posts here, and I’ve already got some domains (the mac server services) reliably proxied by caddy.
Now, to kick it up a notch, I’d like caddy to try hitting a varnish server, AND if the varnish server is unavailable for some reason, caddy serve the request itself.
so
proxy / varnish.backend caddyitself {
policy first
transparent
}
somehow, in my mind, I want to nest a server block within, but I know that’s not right.
I realize that the block above is dumb as written because it is recursive but I think what I want is if for some reason varnish is unavailable, I want caddy to serve it instead.
As I understand the proxy now, if it fails, caddy will mark it unhealthy and not serve any requests. That’s the part I want to fix: I want caddy to be the last resort to handle the req because if it is there, it could do it.
It’s probably possible with a clever rewrite. You could proxy to some subpath (and configure varnish accordingly, perhaps to strip the subpath prefix part), and then also configure Caddy to handle requests to that subpath differently.
But it’s not a native feature of the proxy middleware… yet?
It’s a trivial task if you’re open to using multiple Caddy instances, but I can’t think my way around achieving this in a single Caddyfile, even with rewrite shenanigans.
I thought about the second caddy instance but then I’m back to having to test for whether that instance is running and what do I do if that is gone too?
Meanwhile, this first instance of caddy is sitting there with nothing to do and capable hands to do what needs done.
the rewrite sequence scares me a bit because it would mean that I have to write some conditional VCL language. I’m sure that is easy enough but as much as VCL seems easy, it’s strikes me more like getting it to work requires some element of voodoo
Meanwhile, I’m feeling better about caddy. I thought about the rewrite tricks I’ve seen you guys doing but I couldn’t think of a way to make a double instance from a rewrite.
Two instances as @Whitestrake suggested isn’t a bad idea though.
The first instance would proxy to varnish OR caddy on another port, just as you’ve set up in your original post.
The second instance would be hit only if varnish is down.
It would be running on the same machine, so probably highly unlikely to go down when the first instance (doing the proxying) is still up. I’d go with this setup and see if this really doesn’t work for you. Why make it more complex than it has to be?