2

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.

zsquare
  • 9,916
  • 6
  • 53
  • 87

2 Answers2

1

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 example@mail.com"
        # i guess we can use other directives like redirect 
    }
}
Medveddo
  • 11
  • 1
0

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.

nuts
  • 715
  • 8
  • 22