As simple as it sounds, a maintenance page is something we cannot do without if we want to provide a quality user experience. Googling around has led me to find some solutions, but they violated some of the nginx best practices, particularly the if directive. Many bad things and unexpected behaviors can happen with using in the nginx config.

After some researching and experimentation, I discovered an simple and elegant solution for posting a maintenance page for nginx and Passenger without using ifs and rewrites in the nginx.conf file.

Use something like this in nginx.conf, in the server block:

try_files $uri /maintenance.html @passenger;
passenger_enabled on;
root /data/
location @passenger {
  # lolwut? we need this for try_files
  passenger_enabled on;
  root /data/
}

Simply symlink maintenance.html when you need to go offline, and remove it to go back to normal operation.

Advertisements