I have a fairly simple server setup for which the configuration at the end of the answer will do the following:
If a file named MAINTENANCE
exists one level above the root, then the server will be in maintenance mode. (This is the -f
test).
Except that if there exist a cookie named foo_dev
with the value secret!
, the check described above will be bypassed and nginx will serve the data as if the server were not in maintenance mode. (This is the $cookie_foo_dev = "secret!"
test.)
I've tried with some try_files
settings and did not run into any issue.
Here is the configuration.
server {
# ... omitted stuff that does not pertain to the solution ...
root <some path>;
if ($cookie_foo_dev = "secret!") {
break;
}
if ( -f $document_root/../MAINTENANCE) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
location / {
proxy_pass http://localhost:8000;
}
}
All incoming connections are required to use HTTPS so the cookie never goes in plain text over the Internet.
I've also set the server to which nginx forwards the requests to display a big fat warning if the cookie foo_dev
is set so that I don't mistakenly forget that the site is in maintenance mode.