Static error page served by nginx when my application is down

If my (Rails) application is down or undergoing database maintenance or whatever, I'd like to specify at the nginx level to serve a static page. So every URL like http://example.com/* should serve a static html file, like /var/www/example/foo.html.

Trying to specify that in my nginx config is giving me fits and infinite loops and whatnot.

I'm trying things like

location / {
  root /var/www/example;
  index foo.html;
  rewrite ^/.+$ foo.html;
}

How would you get every URL on your domain to serve a single static file?


Solution 1:

Add two locations like this:

location = /foo.htm {
  root /var/www/example;
  index foo.html;
}

location / {
  rewrite ^/.+$ /foo.htm;
}

Solution 2:

I am not 100% sure but if the Rails server fails there is an error 500. Maybe you could use the error_page directive like

error_page 500 /staticpage.html