Capistrano and nginx


#1

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


#2

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.


#3

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.


#4

removed_email_address@domain.invalid 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.


#5

or you can also put your pictures and css on a different server :wink:
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 M. wrote:

removed_email_address@domain.invalid 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.