Static files and 404

Hi,

I have a setup where I am using nginx with RubyOnRails.

The setup looks simple:
if (!-f $request_filename) {
proxy_pass http://domain1;
break;
}

However I have a directory that holds images that sometimes get deleted.
People keep linking to those images and the 404s go to the rails
process.

How can I tell nginx that any request towards:
/images/subassets/*.gif
should never be forwarded to rails?

Here’s a simplified version of I use the following to accomplish this,
inside my location block:

if ($uri ~* (.css|.js|.ico|.gif|.jpg|.png)) {
break;
}

Note that I haven’t tested this simplified form directly, but I believe
it
should work.

You can also turn that into a regex location (or location with captures
in
0.7) and take it out of the main location block. I am not 100% sure,
but my
intuition tells me that the location block performs much (well, at least
a
little) better than the if block. It looks cleaner, anyway :).

  • Merlin

On Thu, 2009-03-19 at 20:14 +0200, Marcelo B. wrote:

However I have a directory that holds images that sometimes get deleted.
People keep linking to those images and the 404s go to the rails process.

How can I tell nginx that any request towards:
/images/subassets/*.gif
should never be forwarded to rails?

location ~ /images/subassets/*.gif {
root /path/to/root;
}

location / {
root /path/to/root;
error_page 404 = @rails;
}

location @rails {
proxy_pass http://domain.com;
}

If you don’t want anything from the subassets folder (not just gifs)
to be passed to Rails, then just use

location /images/subassets {
root /path/to/root;
}

Cliff

On Thu, 2009-03-19 at 13:21 -0700, Cliff W. wrote:

Oops, should be

  • location ~ /images/subassets/*.gif {
  • location ~ /images/subassets/*..gif$ {

On Thu, Mar 19, 2009 at 12:18:50PM -0700, Merlin wrote:

You can also turn that into a regex location (or location with captures in
0.7) and take it out of the main location block. I am not 100% sure, but my
intuition tells me that the location block performs much (well, at least a
little) better than the if block. It looks cleaner, anyway :).

Yes, better and much, much cleaner.

On Thu, Mar 19, 2009 at 01:26:29PM -0500, Nick P. wrote:

Here’s a simplified version of I use the following to accomplish this,
inside my location block:

if ($uri ~* (.css|.js|.ico|.gif|.jpg|.png)) {
break;
}

Note that I haven’t tested this simplified form directly, but I believe it
should work.

“if ($uri ~” or “if ($request_filename ~” means that you should use
locations as here:

location / {

}

location ~* .(?:css|js|ico|gif|jpg|png)$ {

}

On Thu, Mar 19, 2009 at 01:37:08PM -0700, Cliff W. wrote:

location ~ /images/subassets/.*.gif$ {

Yes :slight_smile:

location ~ ^/images/subassets/.*.gif$ {

Hi,

I placed something like:

location ~
/images/(tassets|bassets)/(fullsize|thumb)/.+(.gif|.jpg|.png) {
root /path/to/root/;
}

Images stopped being loaded after that.

The only weird thing is that both tassets/ and bassets/ are symlinks.

On Thu, 2009-03-19 at 13:22 -0700, Cliff W. wrote:

On Thu, 2009-03-19 at 13:21 -0700, Cliff W. wrote:

Not a good regex day for me…

Oops, should be

  • location ~ /images/subassets/*.gif {
  • location ~ /images/subassets/*..gif$ {

location ~ /images/subassets/.*.gif$ {

Cliff

What does error log say?

Never mind.

It worked! I entered the root variable incorrectly.

Thanks!