静态文件服务只适用于root是公共下的子文件夹

我正在尝试使用nginx来提供静态caching文件。 在rails_root/public/cache目录下有index.html文件。 我首先尝试了下面的configuration,这不起作用:

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

这给错误:

 [error] 4056#0: *13503414 directory index of "(...)current/public/" is forbidden, request: "GET / HTTP/1.1" 

我然后尝试

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

而令我惊讶的是这个作品。 为什么我可以做后者而不是前者(因为他们指向同一地点)

文件系统的权限

这些文件夹的权限是:

 775 public 755 cache 644 index.html 

Rails坐在用户目录下,所以文件夹和文件都属于用户good 。 nginx的用户对于每个工作处理器来说是master和http root

 89:http 7865 0.1 0.0 8876 2624 ? S Jul10 0:51 nginx: worker process 112:root 24927 0.0 0.0 8532 1828 ? Ss Jun28 0:03 nginx: master process /usr/sbin/nginx -c /etc/nginx/conf/nginx.conf 

我的图标坐在公共/正确的下列服务:

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

LOG

对于我的工作设置:访问日志显示:

 request:"GET / HTTP/1.1" 200 uri:"/index.html" request:"GET /assets/gas/468x15_4.gif HTTP/1.1" 200 uri:"/assets/gas/468x15_4.gif" 

如果我添加index nonexistent相同的目录索引禁止的错误将出现以下访问日志:

 request:"GET / HTTP/1.1" 403 uri:"/" 

错误日志:

 directory index of "RAILS_ROOT/current/public/cache/gas/" is forbidden, client: _IP_, server: example.com, request: "GET / HTTP/1.1", host: "gas.example.com" 

UPDATE

请看完整的configuration文件 。 注意它是完整的,上面给出的例子有点简化。

我正在使用Nginx 1.22

问题在于location块中的if指令。 这些只在完整的configuration文件中显示(对于那些试图解决这个问题没有的人抱歉)。 一旦我将它们移动到server块级别,它工作正常。

directory index of *** is forbidden表示在位置块中没有内容指令可用。 在这种情况下,该location是一个嵌套的 location块,在location块中使用if指令时创build。

ifserver块级别的指令没有这个问题。 我if s可以被移动,并仍然正常工作,但我不知道如果移动s不会工作。

所以在这种情况下,文件权限可能不是那么相关。

@Vbart是正确的说如果是邪恶的 。

我认为索引应该在NGX_HTTP_ACCESS_PHASE附近的try_files之前处理,因为日志build议你正在请求目录。

正确的方法是打这个问题的官方邮件列表。