tags:

views:

722

answers:

1

I have a Rails app which generates a lot of images; ie an images controller with a show action fetches an image from a SAN and serves it. I have a caches_page on this which reliably saves a copy of the file in a custom location; RAILS_ROOT + /public/cache. The files are there and look fine.

I am serving the app through nginx and it seems to work fine but for the life of me I can't get it to send those cached image files instead of hitting the rails action. I'm pretty new to nginx and have been researching it for some time but not matter what I try it doesn't work - I'm doing something fundamentally wrong.

To clarify, I have a uri like this:

/images/show/123.jpg

the file may exist here:

/cache/images/show/123.jpg

How can I make nginx serve that file and not pass on the request?

Here's an incomplete list of what I've tried:

if (-f /cache$request_filename) { 
  rewrite (.*) /cache$1 break;
  break; 
}

if (-f /cache$uri.jpg) {
rewrite (.*) /cache$1.jpg break;
}

if (-f /cache/images/show/$request_filename) {
rewrite (.*) /cache/images/show/$1.jpg break;
}

Any ideas?

update: I answered my own question. The correct rule was:

      if (-f $document_root/cache$request_uri) {
      rewrite (.*) /cache$1 break;
      }
A: 

You could also cache images directly on Nginx using MemcachedModule.

Sebastjan Trepča