Derek Lawless

There is always sunshine / Far above the grey sky

Configuring a maintenance page in NGINX is quite straightforward and, if configured correctly, allows for live toggling on and off without requiring the NGINX instance to be reloaded or restarted.

location / {
	# Check for the existence of an enabled maintenance page
	if (-f {PATH}/{TO}/maintenance-page/index-on.html) {
		return 503;
	}

	# Include the rest of this location's configuration as normal
}

location /maintenance-page {
  root {PATH}/{TO}/{MAINTENANCE_PAGE_PARENT_FOLDER};
}

# Serve out the maintenance page for 503, 504 errors.
error_page 503 504 /maintenance-page/index-on.html;

For each inbound request matched by this location, NGINX will check for the existence of an index-on.html page - if found, it will be served with an HTTP 503 status code.

To toggle the maintenance page on or off, simply rename the maintenance page:

cd {PATH}/{TO}/maintenance-page
mv index-off.html index-on.html
cd {PATH}/{TO}/maintenance-page
mv index-on.html index-off.html
© 2022 Derek Lawless. Built with Gatsby