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

**URL:** <https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738>\
**Category:** NGINX\
**Created:** [August 16, 2011, 9:23am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738 "2011-08-16T09:23:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 16, 2011, 9:23am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/1 "2011-08-16T09:23:39Z")

</div>

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](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/](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)

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 16, 2011, 10:54am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/2 "2011-08-16T10:54:45Z")

</div>

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

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 16, 2011, 10:47pm UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/3 "2011-08-16T22:47:31Z")

</div>

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](http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw)

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 16, 2011, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/4 "2011-08-16T23:12:48Z")

</div>

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](http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw)

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

Maxim D.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 17, 2011, 12:00am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/5 "2011-08-17T00:00:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 17, 2011, 12:06am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/6 "2011-08-17T00:06:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 16, 2011, 11:25pm UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/7 "2011-08-16T23:25:08Z")

</div>

On Tue, Aug 16, 2011 at 4:12 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [August 17, 2011, 12:26am UTC](https://www.ruby-forum.com/t/how-to-make-nginx-find-my-assets-that-are-not-in-my-public/210738/8 "2011-08-17T00:26:45Z")

</div>

On Tue, Aug 16, 2011 at 5:05 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> try\_files $uri $uri.html $uri/index.html @app;

Thanks again, this is how it looks like now:

[http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw](http://pastie.org/private/8jat5h9kdagmvtxxzr9pcw)
