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

Posted by lulalala on Server Fault See other posts from Server Fault or by lulalala
Published on 2012-07-06T02:44:40Z Indexed on 2012/07/06 3:17 UTC
Read the original article Hit count: 472

Filed under:
|

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;

# $uri always contains one slash(the first slash but not the last)
try_files /cache$uri/index.html /cache$uri.html @rails;

This give 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;

# $uri always contains one slash(the first slash but not the last)
try_files $uri/index.html $uri.html @rails;

And to my surprise this works. Why is it that I can do the latter not the former( since they point to the same location)


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;
}

© Server Fault or respective owner

Related posts about nginx

Related posts about ruby-on-rails