How to set only specific nginx server block into maintenance mode programmatically
- by Ville Mattila
I am looking for a solution to automate one of our application's deployment process. In the beginning of deployment, I would like to programmatically set the specified server into maintenance mode and finally after the deployment has been completed, remove the maintenance mode flag from the nginx server.
By maintenance mode, I mean that nginx should response with HTTP Response Code 503 to all the requests (with possible custom page).
I know how to set the server block to respond with 503 code (see http://www.cyberciti.biz/faq/custom-nginx-maintenance-page-with-http503/) but the question is about how to do this programmatically and most efficiently.
Two options have came to my mind:
Option 1: At the beginning of the deployment process, write a maintenance file into document root and conditionally check an existence of the maintenance file in nginx server config:
server {
if (-f $document_root/in_maintenance_mode) {
return 503;
}
}
This method contains certain overhead as the file existence is checked for each request. Is it possible to check the file existence only when loading the nginx config?
Option 2: Deployment script replaces the whole nginx server configuration file with a maintenance version and swaps it back in the end of the deployment. If this method is used, I am concerned about possible other automation processes like puppet that may be override the maintenance configuration file.