Moving discussion from #260.
Here's my proposal from my comment there:
A common nginx pattern is to perform "does maintenance.html exist? if it does, return 503", and it seems it wasn't discussed.
An exists keyword would be a nice addition which would solve the problem in this space.
rewrite {
r (.*)
to /maintenance.html
if exists /path/to/maintenance.html
}
status /maintenance.html 503
However it feels like the logic is the wrong way around. A better solution, imho:
status 503 {
/
if exists /path/to/maintenance.html
body /path/to/maintenance.html
}
(I'm a little surprised that status doesn't yet support writing a body, but maybe I'm missing something from the docs)
If status simply set the response code and let the request be handled normally otherwise, that should resolve this, right? rewrite can be used to rewrite to a certain page _if it exists_ and then status would, in conjunction, set the response code to 503.
I don't think so because that is basically offloading the serving of the maintenance mode. The advantage of the maintenance mode in nginx is to have it contained to whatever is bound to port 80, so with as few dependencies as possible.
It'd still be bound to the same port. Imagine:
rewrite {
to /maintenance.html {path} {path}/
}
status 503 /maintenance.html
If the status middleware was adjusted so that it passed the request through, Caddy would rewrite all requests to /maintenance.html, serve that page, but with a response code of 503. No offloading, no dependencies.
Ah, I see. rewrite only does so if the path works? If that's the case, I think it could work.
@jleclanche Yeah, using to like that will only select that one if the file exists.
I'll close this issue now since it seems to do what you're looking for! Thanks to @abiosoft for implementing that feature a while ago.
Does it really work for you? I tried:
http://example.com{
root /path
rewrite {
to /file.html
}
status 503 /file.html
}
but it does not work. This file simply returns HTTP status 503 and 503 Service Unavailable. If I comment out the status 503 /file.html, content of /file.html is returned, but with HTTP status 200 (which is expected, because status was not set, but proves that /file.html exists and is readable).
It'd still be bound to the same port. Imagine:
rewrite {
to /maintenance.html {path} {path}/
}
status 503 /maintenance.html
If the status middleware was adjusted so that it passed the request through, Caddy would rewrite all requests to /maintenance.html, serve that page, but with a response code of 503. No offloading, no dependencies.
Does not seem to work as stated. The maintenance.html is not served. Just returns 503 Service Unavailable
@mholt
Did anyone get this working as expected?
Hi, if you are having difficulties with this can you create a new issue, or ask for help on https://caddy.community thanks
Most helpful comment
Ah, I see.
rewriteonly does so if the path works? If that's the case, I think it could work.