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

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: