我正在尝试设置nginx,以便某个url生成我的服务器上某个目录的目录索引。 目前这是我的default.conf的样子。
location / { root /usr/share/nginx/html; index index.html index.htm; } location /files/ { root /home/myfiles/Downloads; autoindex on; }
但是,当我尝试去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",
我不希望它search文件/ index.html,我只想要下载的目录索引。 我需要改变什么才能以这种方式进行工作?
检查Nginx是否对父目录树中的所有目录执行权限。 在你的情况下,Nginx应该拥有对/ , /home , /home/myfiles , /home/myfiles/Downloads执行权限,否则Nginx不能对这些目录进行chdir。
一个简单的方法来检查这是运行namei -l /home/myfiles/Downloads 。
在这种情况下,您需要使用alias指令而不是root 。
在使用root , mysite.com/files/ /home/myfiles/Downloads/files/的请求会在本地目录/home/myfiles/Downloads/files/查找索引文件,如果找不到则会自动生成目录列表(因为您指定了autoindex选项)。 注意nginx如何将 /files/ 附加到你指定的根目录。
由于您的情况下,您希望/files/成为您的下载目录的同义词,您需要在位置块中使用alias /home/myfiles/Downloads/ 。 然后, mysite.com/files/ /home/myfiles/Downloads/任何请求将被转换到/home/myfiles/Downloads/ (例如, mysite.com/files/bigfile.gz /home/myfiles/Downloads/ mysite.com/files/bigfile.gz将成为/home/myfiles/Downloads/bigfile.gz )。 请注意,尾随/在别名是必要的。
有关别名的更多详细信息,请参阅http://wiki.nginx.org/HttpCoreModule#alias文档。
您好我也有autoindex错误。 顺便说一句:我安装nginx使用第三方回购。
/opt/tengine/sbin/nginx -V 2>&1 | grep auto
[root@sec002 tengine]# /opt/tengine/sbin/nginx -V 2>&1 | grep auto ngx_http_autoindex_module (static)
server { listen 8082; server_name sec002.xxx.com; root /tmp; autoindex on; location / { root /tmp/php/; autoindex on; autoindex_exact_size off; autoindex_localtime on; } }
我浏览器sec002.xxx.com:8082,错误如下:
2016/01/07 16:09:26 [error] 4205#0: *1 "/tmp/php/index.html" is not found (2: No such file or directory), client: 192.168.2.13, server: sec002.xxx.com, request: "GET / HTTP/1.1", host: "sec002.xxx.com:8082"
我确保执行权限和nginx用户是正确的,它是werid
最后,我放弃并用默认的tar包重buildnginx。 那么它的工作
根据nginx文档 :
“当ngx_http_index_module找不到索引文件时,请求只能到达ngx_http_autoindex_module。
如果某个位置没有定义index ,则默认为这些值: index index.html 。
从/home/myfiles/Downloads/files/删除index.html,它应该显示目录列表中的文件