How to make nginx find my assets that are not in my public/?

Hi,

So I a rails app that use nginx serving all my assets from my public
directory, but my web app also serves some custom assets directly from
the db. The problem is that nginx tries to look those assets in my
public directory instead of getting them from the app server.
Basically, all the custom assets start with the same path:
“/_files/*”. Other assets are served from /images, /stylesheets etc.

Here is my conf:http://pastie.org/private/lrhvnko4hjexx5wbqbr4w

This is the error I get:

2011/08/16 01:42:06 [error] 17826#0: *40 open()
“/home/pat/gitreps/shapado/public/_files/groups/logo/4e48b6f9b002e261db000010.png”
failed (2: No such file or directory), client: 127.0.0.1, server:
localhost.lan, request: “GET
/_files/groups/logo/4e48b6f9b002e261db000010.png HTTP/1.1”, host:
“localhost.lan”, referrer: “http://localhost.lan/

Thanks in advance,

Pat

PS: when I use rails with the basic app server the assets are served
fine (as it all goes through rails app server I guess)

Hello!

On Tue, Aug 16, 2011 at 02:23:23AM -0500, Patrick A. wrote:

This is the error I get:

2011/08/16 01:42:06 [error] 17826#0: *40 open()

“/home/pat/gitreps/shapado/public/_files/groups/logo/4e48b6f9b002e261db000010.png”

failed (2: No such file or directory), client: 127.0.0.1, server:
localhost.lan, request: “GET
/_files/groups/logo/4e48b6f9b002e261db000010.png HTTP/1.1”, host:
“localhost.lan”, referrer: “http://localhost.lan/

You told nginx to server any png files from disk:

location ~* \.(ico|css|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires max;
    break;
}

(just a side note: “break” is not needed here, and “?..” check is
usesless as location directive only checks path, not query string)

There is no surprise nginx does what you said and tries to serve
them from disk.

Looking though your config suggests something like this should be
what you really want:

location / {
    # try files on disk; if not found, go to app server
    try_files $uri $uri.html $uri/ @app;
}

location @app {
    proxy_pass http://shapado_app_server;
}

location /images/ {
    # serve from disk and set expires
    expires max;
}

location /stylesheets/ {
    expires max;
}

location /javascripts/ {
    expires max;
}

location /system/ {
    expires max;
}

Please see here for more details:

http://wiki.nginx.org/HttpCoreModule#location
http://wiki.nginx.org/HttpCoreModule#try_files

Maxim D.

Thanks a lot Maxim, this worked great. This is how my conf looks like
now, not sure if it’s perfect yet but for anyone interested:

http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw

Hello!

On Tue, Aug 16, 2011 at 03:47:05PM -0500, Patrick A. wrote:

Thanks a lot Maxim, this worked great. This is how my conf looks like
now, not sure if it’s perfect yet but for anyone interested:

http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw

You don’t need all these if’s in “location /”, try_files is to
replace them all.

Maxim D.

Ok, got it working like that:
http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw

Hello!

On Tue, Aug 16, 2011 at 04:24:26PM -0500, Patrick A. wrote:

You don’t need all these if’s in “location /”, try_files is to
replace them all.

I get this error when I remove those ifs:

2011/08/16 16:23:17 [error] 9352#0: *1 directory index of
“/home/pat/gitreps/shapado/public/” is forbidden, client: 127.0.0.1,
server: localhost.lan, request: “GET / HTTP/1.1”, host:
“localhost.lan”

Ah, you need directories without index.html to be passed to app
server as well, right? Then use

location / {
    try_files $uri $uri.html $uri/index.html @app;
}

location @app {
    proxy_pass http://shapado_app_server;
    proxy_set_header ...
}

...

Maxim D.

On Tue, Aug 16, 2011 at 4:12 PM, Maxim D. removed[email protected]
wrote:

replace them all.

I get this error when I remove those ifs:

2011/08/16 16:23:17 [error] 9352#0: *1 directory index of
“/home/pat/gitreps/shapado/public/” is forbidden, client: 127.0.0.1,
server: localhost.lan, request: “GET / HTTP/1.1”, host:
“localhost.lan”

On Tue, Aug 16, 2011 at 5:05 PM, Maxim D. [email protected]
wrote:

try_files $uri $uri.html $uri/index.html @app;

Thanks again, this is how it looks like now:

http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw