Serving maintenance page with 503 status code in Caddy

1.5k Views Asked by At

I'm trying to do something which seems fairly trivial to me, but can't seem to get it right with Caddy.

I have the following site configured with Caddy:

foo.com {
  tls {
    dns cloudflare ...
  }
  reverse_proxy /* http://proxy-foo
}

I'm now trying to enable a maintenance page, such that all requests serve the maintenance html, and return a 503 status code. But, I can either serve the page, or the status code, but not both.

I first tried the handle_errors directive, with a respond directive

foo.com {
  tls {
    dns cloudflare ...
  }
  # reverse_proxy /* http://proxy-foo
  handle_errors {
    rewrite * /503.html
    file_server
  }
  respond * 503
}

only to later read the caveat that respond doesn't trigger the error handlers.

Also tried removing all other directives, thinking that would trigger a 404, which would in turn call the handle_errors block, but that too doesn't work. It just ends up returning 200, but with no body.

Any pointers as to where I'm going wrong much appreciated.

2

There are 2 best solutions below

0
On

You can try configure reverse_proxy directive.

That code similar to docs example is worked for me:

:80

reverse_proxy localhost:8000 {
    @error status 500 503
    handle_response @error {
        respond "Something bad happened. If the error occurs again, please contact me at [email protected]"
        # i guess we can use other directives like redirect 
    }
}
1
On

So apparently caddy can create a matchers logic for a file like so:


@hasMaintenance file /path/to/your/maintenance_file.html
handle @hasMaintenance {
    try_files /path/to/your/maintenance_file.html
    file_server {
        status 503
    }
}

This only returns the maintenance file with a 503 status code, if it is found by the matcher. Otherwise continue as usual.