Setting a maintenance page with Nginx
I recently had a need to set a maintenance page with Nginx. The process was simple to redirect everything and return a 503 (Service Unavailable), but a problem developed when the maintenance page needed access to the stylesheets and images of the main site. So we could no longer use a simple ‘redirect everything’ rule. I searched for an answer and couldn’t find anything that seemed right. Most of the answers were everything from ‘use inline stylesheets’ to ‘serve the images and css from another server.’ After a bit of tinkering, I came up with a very simple solution.
In the nginx.conf file, in your server block, define a location for every directory that the site will need access to (see below – stylesheets and images) before you define the / location where you return the 503 (which must be last). Now the page can access the bits of the site it needs and return a 503 for everything else.
server {
listen 80;
server_name www.mysite.com;
root /apps/web/workspace/maintenance.mysite.com;
location /stylesheets {
}
location /images {
}
location / {
return 503;
}
access_log /apps/nginx/logs/www.mysite.com_access.log;
error_log /apps/nginx/logs/www.mysite.com_error.log notice;
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maint.html break;
}
}

Great stuff! I’ve been trying for the best part of a day to get my custom 503 page in Nginx to load CSS and image files and your solution worked a treat.