Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Did one of the answers solve your problem? –  Christopher Perrin Jul 5 '12 at 8:46
add comment

4 Answers 4

up vote 3 down vote accepted

Add two locations like this:

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

location / {
  rewrite ^/.+$ /foo.htm;
}
share|improve this answer
    
This worked for me. (I used ^/.*$ as the regexp.) –  dreeves Jul 17 '12 at 19:59
add comment

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
share|improve this answer
    
Ah, that's smart, and I should do that too, but I'd still like to get nginx to cooperate with a catchall rewrite. The reason is that I may need to flip that on when the Rails app isn't actually down yet but we're migrating the database or something. Thanks for pointing out error_page though! –  dreeves Jun 22 '12 at 1:49
add comment

Use named location, it's doesn't change URI during redirect.

This snippet processes "rails is down" situation.

error_page 504 @rubydown; # 504 - gateway timeout

location @rubydown {
    internal;
    root /var/www;
    rewrite ^ /504.html break;
}

For maintaince notification you can use something similar in root location ...

location / {
    root /var/www;
    try_files /maintaince.html @rails;
}

location @rails {
    internal;
    proxy_pass http://rails.backend;
    # blablabla proxy_pass setting for Rails
}

Create file /var/www/maintaince.html.tmpl, write desired text. And before maintaince work create simlink similar ln -s /var/www/maintaince.html.tmpl /var/www/maintaince.html or just rename file. When maintaince work has done, remove simlink or rename file back.

share|improve this answer
    
That's clever and convenient! But it seems like it entails a tiny performance hit during normal operation, checking for the maintenance.html page on every pageview. Does that sound correct or is there clever caching or something that makes it really not an issue at all? –  dreeves Jul 16 '12 at 3:57
    
Sorry for "necro" comment, I did not notice this issue in July. Not issue at all, don't worry. This "right way" for such use case, recommended by Igor Sysoev (nginx author) by himself at nginx's russian maillist. –  cadmi Sep 20 '12 at 9:06
add comment

The following should redirect all the requests to /var/www/example/foo.html.

location / {
  root /var/www/example;
  index foo.html;
  # In the following try_files directive, the request will never reach =404.
  try_files /foo.html =404;
}
share|improve this answer
    
I think this introduces a redirect loop. –  dreeves Jul 17 '12 at 19:35
    
@dreeves Please check my updated answer! –  Pothi Jul 17 '12 at 20:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.