Redirecting to a custom Nginx Maintenance Page

Here’s a little howto in case you need to take your site offline and want to redirect all its traffic to a “down for maintenance” page. For search engine friendliness, it’s important that your server returns a 503: Service Unavailable status code. Following you can find the way I solved it using Nginx:

server {
        listen      80;
        server_name joergfelser.at;
        root    /var/www/joergfelser.at/;

        location / {
            if (-f $document_root/maintenance.html) {
                return 503;
           }
            ... # rest of your config, it's important to have 
            ... # the maintenance case at the very top
         }

        error_page 503 @maintenance;
        location @maintenance {
                rewrite ^(.*)$ /maintenance.html break;
        }
}

Now whenever you need to take your site offline you simply move your maintenance.html into the $document_root (in this case /var/ww/joergfelser.at). As soon as the file exists nginx will redirect all following requests to your maintenance page.