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

Maxim Dounin mdounin at
Tue Aug 16 08:54:03 UTC 2011


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

> 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:
> 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:, 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;

(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:

Maxim Dounin

More information about the nginx mailing list