Ruby Forum Rails deployment > Capistrano and nginx

Posted by toulax@gmail.com (Guest)
on 01.07.2007 09:53
(Received via mailing list)
I'm using nginx with capistrano to deploy my Rails app and I'm having
a hard time getting the maintenance capistrano task
(deploy:web:disable) to work like I want. I want nginx to redirect all
requests to public/maintenance-on.html if the file exists, however,
since I use stylesheets and images on this file, nginx should serve
them. Here's what I have so far on nginx.conf:

if (-f $document_root/public/maintenance-on.html) {
  rewrite ^(.*)$ /maintenance-on.html last;
  break;
}

How could I make this not match urls containing /stylesheets/ and /
images/?

Thanks in advance
Posted by moomerman@gmail.com (Guest)
on 02.07.2007 19:05
(Received via mailing list)
The rewrite directive you pasted means all requests will be rewritten
which would include stylesheets and images.  Just make sure this
directive appears in your nginx configuration before the part where
you serve static files or proxy to mongrel.

What you have written says:

'if the file $document_root/public/maintenance-on.html exists rewrite
ALL requests to serve the maintenance-on.html page'

Richard.
Posted by toulax@gmail.com (Guest)
on 02.07.2007 22:58
(Received via mailing list)
Richard,

That was actually the first thing I tried, but even adding the
maintenance after the rule that make nginx serve statics files the
maintenance always comes first.
Posted by Eli Miller (jqr)
on 29.11.2007 22:18
toulax@gmail.com wrote:
> (deploy:web:disable) to work like I want. I want nginx to redirect all
> requests to public/maintenance-on.html if the file exists, however,
> since I use stylesheets and images on this file, nginx should serve
> them.

Here is a solution I've used before:

  if (-f $document_root/system/maintenance.html) {
    set $maintenance 1;
  }
  if ($request_uri ~* (jpg|jpeg|gif|js|css)$) {
    set $maintenance 0;
  }
  if ($maintenance) {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

You can change the regex that $request_uri is matched against to match 
your site quite easily.
Posted by D. Krmpotic (rubypassion)
on 18.02.2008 23:43
or you can also put your pictures and css on a different server ;)
I struggled with this for too many hours and couldn't make it work.. not 
even with the xample from Eli.. The problem was that assets were 
requested from a rul that wasn't exact (one level above etc. - depending 
on what the original request was).. Maybe I should try again, but for 
now I'm happy with my stupid solution.

Eli Miller wrote:
> toulax@gmail.com wrote:
>> (deploy:web:disable) to work like I want. I want nginx to redirect all
>> requests to public/maintenance-on.html if the file exists, however,
>> since I use stylesheets and images on this file, nginx should serve
>> them.
> 
> Here is a solution I've used before:
> 
>   if (-f $document_root/system/maintenance.html) {
>     set $maintenance 1;
>   }
>   if ($request_uri ~* (jpg|jpeg|gif|js|css)$) {
>     set $maintenance 0;
>   }
>   if ($maintenance) {
>     rewrite  ^(.*)$  /system/maintenance.html last;
>     break;
>   }
> 
> You can change the regex that $request_uri is matched against to match 
> your site quite easily.