Maintenance page on nginx, best practices

I think the best practice would be returning a 500 status code.

I think you mean 503 instead of 500.

they use if to make the redirect and according to nginx documentation it isn't safe to use ifs.

No. Only return is 100% safe inside if in location context.

According to nginx documentation, you can specify an HTTP status code as the last argument to try_files. I've tried this but it didn't work.


Here is what I do.

            if (-f $document_root/maintenance.html) {
                    return 503;
            }
            error_page 503 @maintenance;
            location @maintenance {
                    rewrite ^(.*)$ /maintenance.html break;
            }

If the file is there it will show the maintenance page. Once you remove the file you will go back to normal.


Yes, it's important to use HTTP 503 for temp. redirects. That's the way I've solved it:

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;
        }
}

I've also written a blog post on that topic:
https://www.joergfelser.at/redirecting-to-a-custom-nginx-maintenance-page/

Happy maintenancing ;)