caddy: Custom error page when upstream no available

Hey!

I was able to configure a custom error page when upstream generates an error (say, 500 “Internal Server Error”). But it seems not possible to send a custom page, like 502.html when upstream is not available. I have found old discussion on community forum saying that it’s a limitation of current version. But maybe anything changed since that? Is it documented somewhere? Please advice.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

(No upstream available is 503 btw. You can use a matcher – also exhibited in the examples on that page – to limit to just that status code if you want.)

Really?

>curl -v https://beta.my.site
*   Trying 1.1.1.1:443...
* Connected to beta.my.site (1.1.1.1) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: ALPN, offering http/1.1
* schannel: ALPN, server accepted to use http/1.1
> GET / HTTP/1.1
> Host: beta.my.site
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 502 Bad Gateway
< Server: Caddy
< Date: Thu, 14 Jul 2022 00:32:57 GMT
< Content-Length: 0
<
* Connection #0 to host beta.my.site left intact

Firefox: изображение

I just want to point out… did you notice that the docs page Francis linked to has an example (the very first one, in fact) that does exactly what you are asking? If it’s not what you’re asking, then we need you to fill out the help template on the forum because clearly we need more information to understand your question:

Maybe it’s a question on how docs are structured. I was looking at this because I thought it’s more relevant: https://caddyserver.com/docs/caddyfile/directives/reverse_proxy You can press Ctrl-F and look where it mentions error. Actually that’s where my issue started from.

Next I found this: https://caddy.community/t/caddy1-proxy-errors-page/6559/6

Which basically remained unanswered and is linked to the issue: https://github.com/caddyserver/caddy/issues/1447, and it’s not clear whether it’s solved or not and what should I do.

Showing error page when upstream not available is very basic thing and should be pretty simple to my understanding.

@inliquid I appreciate the effort you took to search the forum.

As I said I found info that feature is not supported.

I just want to point out… did you notice that the docs page Francis linked to has an example (the very first one, in fact) that does exactly what you are asking? If it’s not what you’re asking, then we need you to fill out the help template on the forum because clearly we need more information to understand your question:

handle_errors {
	rewrite * /{err.status_code}.html
	file_server
}

(No upstream available is 503 btw. You can use a matcher – also exhibited in the examples on that page – to limit to just that status code if you want.)

It’s more a question not about returning code, but how does caddy treat this case. In my view this is “no upstreams available” because, that’s what happening on the ground - single configured backend is dead. As for codes, like I said I wouldn’t use 503 in my app because it mixes with handler timeouts so I would probably configure it like this:

        handle_errors {
                root * /my/site/html
                rewrite * /maintenance.html
                file_server {
                        status 502
                }
        }

Not sure but should work?