# Capistrano and nginx

**URL:** <https://www.ruby-forum.com/t/capistrano-and-nginx/175821>\
**Category:** Deployment\
**Created:** [July 1, 2007, 9:53am UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821 "2007-07-01T09:53:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 1, 2007, 9:53am UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821/1 "2007-07-01T09:53:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 2, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821/2 "2007-07-02T19:05:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 2, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821/3 "2007-07-02T22:58:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 29, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821/4 "2007-11-29T22:18:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/capistrano-and-nginx/175821/5 "2008-02-18T23:43:16Z")

</div>

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 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.
