Catch Upstream Errors in Reverse Proxy Setup

I’m using Caddy in a reverse proxy setup and would like to catch any errors the backend is generating.
https://github.com/mholt/caddy/issues/1447 seems to describe fully what I’m looking for.
It’s referring to https://github.com/mholt/caddy/issues/1639 that mentions this will not get implemented in the coming year or so.
However all is referring to the error directive.

I still have some hope there might be a workaround, possibly by using rewrite:

 domain.com {
   proxy / http://backend.host.com  {
    except /404.html
   }
   rewrite {
     if {status} is 404
     to /404.html
   }
 }

The above code doesn’t work. Maybe someone can confirm if rewrite could do the trick or if there is currently no way to catch upstream errors.

Hi @liam, welcome to the Caddy community.

The reason that approach didn’t work is that by the time proxy even begins the process of determining whether or not - and where - to connect upstream, all the rewriting has already been completed.

https://github.com/mholt/caddy/blob/13268db5361dd73d5504a7704c60d76d4755a71f/caddyhttp/httpserver/plugin.go#L612-L644

Generally if the request is getting proxied, it’s all in the hands of the upstream server at that point. I can’t think of a method to handle this at the moment, at least until #1639 is implemented.

Hi @Whitestrake,
This wasn’t the answer I’ve been hoping for but now I know for a fact it won’t work. :slight_smile:
Thanks for the welcome and reply.

No worries. Couldn’t hurt to add your voice to the issue comments, just to note you’re interested in this functionality in particular. I don’t think on its own it would be difficult to implement.

If it’s not at all possible to have the upstream server return the correct error pages, you could look into inserting another proxy server like Varnish or Squid to handle them (I believe they’re both capable of this, but other servers might also be suitable). Golang would also lend itself very well to writing a tiny program for this exact purpose (simple proxy, serve a specific static file instead when upstream returns an error).

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