Nginx

nginx 自動索引的問題

  • November 15, 2017

我正在嘗試設置 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",

我不希望它搜尋 files/index.html,我只想要 Downloads 的目錄索引。我需要改變什麼才能使事情以這種方式工作?

檢查 Nginx 是否對父目錄樹中的所有目錄都有*執行權限。*在您的情況下,Nginx 應該對 , , 具有執行權限//home否則/home/myfilesNginx/home/myfiles/Downloads無法 chdir 到這些目錄。

一個簡單的檢查方法是執行namei -l /home/myfiles/Downloads.

在這種情況下,您需要使用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

引用自:https://serverfault.com/questions/347815