baicai

白菜

一个勤奋的代码搬运工!

啟用 nginx 目錄索引,顯示檔案列表。

在 nginx 中,如果特定目錄中沒有 index.html 檔案,則預設會返回 404 Not Found 的錯誤。

但是,Nginx 自動索引模組 —— ngx_http_autoindex_module 模組,提供了一種自動生成列表的方法,添加自動索引非常容易,使用 autoindex on 即可。下面的配置,將在訪問特定請求時返回目錄結構。

官方參考: http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
    server {
            listen   80;
            ... ...
            
            location /index_dir {
            autoindex on;
            }
    }

除了簡單地使用自動索引打開或關閉之外,還可以對其做其他的配置,包括:

    autoindex_exact_size; 顯示輸出的確切檔案大小,還是最接近的KB,MB或GB。預設為on,顯示出檔案的確切大小,單位是bytes。改為off後,顯示出檔案的大概大小,單位是kB或者MB或者GB。
    autoindex_format; 該指令指定Nginx索引列表應以什麼格式輸出。該指令有4個選項:html/xml/json/jsonp。
    autoindex_localtime; 顯示的檔案時間為GMT時間。 注意:改為on後,顯示的檔案時間為檔案的伺服器時間。

使用這幾個配置後配置內容類似於如下內容:

location /index_dir/ {
    root   /data/index_dir/;  
    autoindex on;
    autoindex_exact_size off;
    autoindex_format html;
    autoindex_localtime on;
}

如果有中文目錄的話會出現亂碼問題,所以還需要在下面添加這一句:

charset utf-8;

charset utf-8,gbk;
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。