If the maintenance page only has HTML
No asset file (CSS, JS, ...)
location = @maintenance {
root html/;
# `break`: use this `location` block
rewrite ^ "/maintenance.html" break;
}
location / {
error_page 503 @maintenance;
return 503;
# ...
}
For maintenance page that has asset file (CSS / JS / ...)
The flow of this is:
- If URI is
/
, return 503
- Internal redirect to
/maintenance.html
- If URI found in
maintenance
directory (for asset), return file with status 200
- All other request, redirect to
/
# for handle maintenance web page
server {
listen unix:/var/run/maintenance.nginx.sock;
# for Windows
# listen 8000;
location @index {
# use redirect because if URI has sub-folder ($uri == /path/path/),
# the relative path for asset file (CSS, JS, ...) will be incorrect
rewrite ^ / redirect;
}
location = / {
# maintenance page URI
error_page 503 /maintenance.html;
return 503;
}
# use RegExp because it has higher priority
location / {
root html/maintenance/;
# redirect all URI to index (503) page (except asset URI),
# asset URI still return status 200
try_files $uri @index;
}
}
# original server block
server {
listen 80;
location / {
proxy_pass http://unix:/var/run/maintenance.nginx.sock:;
# for Windows
# proxy_pass http://127.0.0.1:8000;
}
# ...
}
You can put the config in a maintenance.conf
, include maintenance.conf
when in maintenance.
I will not use if (-f <file>)
because it will try to check if the file exist on every request
If you need bypass some IP address
More detail here
Ref: Nginx Maintenance