# Static file serving only works if root is a subfolder under public

**URL:** <https://www.ruby-forum.com/t/static-file-serving-only-works-if-root-is-a-subfolder-under-public/221266>\
**Category:** NGINX\
**Created:** [July 6, 2012, 5:50am UTC](https://www.ruby-forum.com/t/static-file-serving-only-works-if-root-is-a-subfolder-under-public/221266 "2012-07-06T05:50:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 6, 2012, 5:50am UTC](https://www.ruby-forum.com/t/static-file-serving-only-works-if-root-is-a-subfolder-under-public/221266/1 "2012-07-06T05:50:48Z")

</div>

I am trying to serve static cache files using nginx. There are  
index.html files under the rails\_root/public/cache directory. I tried  
the following configuration first, which doesn’t work:

```
root <%= current_path %>/public;
try_files /cache$uri/index.html /cache$uri.html @rails;

```

Accessing the root / gives this error:

```
[error] 4056#0: *13503414 directory index of "(...)current/public/"

```

is forbidden, request: “GET / HTTP/1.1”

I then tried

```
root <%= current_path %>/public/cache;
try_files $uri/index.html $uri.html @rails;

```

And to my surprise this works.

* * *

The permissions of the folders are:

```
775 public
  755 cache
    644 index.html

```

The thing is that my favicon sitting under public/ is served correctly:

```
# asset server
server {
  listen 80;
  server_name assets.<%= server_name %>;
  expires max;
  add_header Cache-Control public;
  charset utf-8;
  root <%= current_path %>/public;
}

```

So, why is it that I can do the latter not the former( since they point  
to the same location)? Thanks.

Posted at Nginx Forum:

> **[Static file serving only works if root is a subfolder under public](https://forum.nginx.org/read.php?2,228318,228318#msg-228318)**
>
> I am trying to serve static cache files using nginx. There are index.html files under the rails\_root/public/cache directory. I tried the following configuration first, which doesn't work: root \<%= current\_path %\>/public; try\_files...
