nginx 自动索引的问题

Var*_*ath 4 configuration nginx web-server

我正在尝试设置 nginx,以便某个 url 在我的服务器上生成某个目录的目录索引。目前这是我的 default.conf 的样子。

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /files/ {
        root        /home/myfiles/Downloads;
        autoindex   on;
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试访问 mysite.com/files/ 或 mysite.com/files 时,出现 403 Forbidden 错误。

查看我看到的错误日志

2012/01/08 16:23:18 [error] 17619#0: *1 "/home/myfiles/Downloads/files/index.html" is forbidden (13: Permission denied), client: some.ip.addr.ess, server: _, request: "GET /files/ HTTP/1.1",
Run Code Online (Sandbox Code Playgroud)

我不希望它搜索 files/index.html,我只想要下载的目录索引。我需要改变什么才能使事情以这种方式工作?

小智 10

在这种情况下,您需要使用alias指令而不是root.

使用时root,请求mysite.com/files/将在本地目录中/home/myfiles/Downloads/files/查找索引文件,如果未找到,将自动生成目录列表(因为您已指定该autoindex选项)。请注意 nginx 如何附加 /files/到您指定的根目录。

由于对于您的情况,您希望/files/成为下载目录的同义词,因此您需要alias /home/myfiles/Downloads/在 location 块中使用。然后,任何对 的请求都mysite.com/files/将被转换为目录/home/myfiles/Downloads/(例如,mysite.com/files/bigfile.gz将变为/home/myfiles/Downloads/bigfile.gz)。请注意,/别名的尾随是必要的。

有关别名的更多详细信息,请参阅文档http://wiki.nginx.org/HttpCoreModule#alias


Ell*_*ist 5

检查 Nginx 是否对父目录树中的所有目录具有执行权限。在您的情况下,Nginx 应该具有/, /home, 的执行权限/home/myfiles/home/myfiles/Downloads否则 Nginx 无法 chdir 到这些目录。

检查这一点的一种简单方法是运行namei -l /home/myfiles/Downloads.