Forum: NGINX static files and 404

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marcelo B. (Guest)
on 2009-03-19 20:26
(Received via mailing list)
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?
Nick P. (Guest)
on 2009-03-19 20:36
(Received via mailing list)
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.
Merlin (Guest)
on 2009-03-19 21:29
(Received via mailing list)
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
Cliff W. (Guest)
on 2009-03-19 22:32
(Received via mailing list)
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
Cliff W. (Guest)
on 2009-03-19 22:32
(Received via mailing list)
On Thu, 2009-03-19 at 13:21 -0700, Cliff W. wrote:


Oops, should be

- location ~ /images/subassets/*.gif {
+ location ~ /images/subassets/*.\.gif$ {
Igor S. (Guest)
on 2009-03-19 22:43
(Received via mailing list)
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.
Igor S. (Guest)
on 2009-03-19 22:43
(Received via mailing list)
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)$ {
    ...
}
Cliff W. (Guest)
on 2009-03-19 23:15
(Received via mailing list)
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
Igor S. (Guest)
on 2009-03-19 23:20
(Received via mailing list)
On Thu, Mar 19, 2009 at 01:37:08PM -0700, Cliff W. wrote:

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

Yes :)

location ~ ^/images/subassets/.*\.gif$ {
Marcelo B. (Guest)
on 2009-03-20 01:24
(Received via mailing list)
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.
Marcelo B. (Guest)
on 2009-03-20 01:43
(Received via mailing list)
Never mind.

It worked! I entered the root variable incorrectly.

Thanks!
Merlin (Guest)
on 2009-03-20 02:01
(Received via mailing list)
What does error log say?
This topic is locked and can not be replied to.