Nginx瀏覽目錄配置及美化


命令 默認值 值域 作用域 EG
autoindex off on:開啟目錄瀏覽;off:關閉目錄瀏覽 http, server, location autoindex on;打開目錄瀏覽功能
autoindex_format html html、xml、json、jsonp 分別用這幾個風格展示目錄 http, server, location autoindex_format html; 以網頁的風格展示目錄內容。該屬性在1.7.9及以上適用
autoindex_exact_size on on:展示文件字節數;off:以可讀的方式顯示文件大小 http, server, location autoindex_exact_size off; 以可讀的方式顯示文件大小,單位為 KB、MB 或者 GB,autoindex_format為html時有效
autoindex_localtime off on、off:是否以服務器的文件時間作為顯示的時間 http, server, location autoindex_localtime on; 以服務器的文件時間作為顯示的時間,autoindex_format為html時有效

完整配置如下:

location /download
{
    root /home/map/www/; #指定目錄所在路徑
    autoindex on; #開啟目錄瀏覽
    autoindex_format html; #以html風格將目錄展示在瀏覽器中
    autoindex_exact_size off; #切換為 off 后,以可讀的方式顯示文件大小,單位為 KB、MB 或者 GB
    autoindex_localtime on; #以服務器的文件時間作為顯示的時間
    charset utf-8,gbk; #展示中文文件名
}

目錄瀏覽美化

ngx-fancyindex

要查看Nginx編譯了哪些模塊,執行以下命令:

2>&1 nginx -V | tr ' ' '\n'|grep module

查看完整的編譯參數:nginx -V

動態編譯添加Nginx模塊

  1. 在GitHub下載最新源碼:ngx-fancyindex
  2. Nginx-Index源碼下載下來后,解壓,放到nginx源碼目錄(/usr/local/nginx)中,執行下面的代碼,編譯:
  3. nginx -V 參數添加 --add-module=ngx-fancyindex-0.4.2
  4. make 這里不要 make install!!
  5. 選擇Fancy Index主題 Nginx-Fancyindex-Theme

Fancy Index 配置

  1. 進入Nginx安裝的web目錄,執行nginx -V,輸出configure arguments: --prefix=/usr/local/nginx,就是這個目錄
  2. git clone https://github.com/lanffy/Nginx-Fancyindex-Theme.git
  3. 在nginx location模塊中添加Fancy Index配置,如下:
location /download
{
    include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf; # 目錄美化配置
    root /home/map/www/; #指定目錄所在路徑
    autoindex on; #開啟目錄瀏覽
    autoindex_format html; #以html風格將目錄展示在瀏覽器中
    autoindex_exact_size off; #切換為 off 后,以可讀的方式顯示文件大小,單位為 KB、MB 或者 GB
    autoindex_localtime on; #以服務器的文件時間作為顯示的時間
    charset utf-8,gbk; #展示中文文件名
}
#fancyindex.conf
fancyindex on;
fancyindex_localtime on;
fancyindex_exact_size off;
fancyindex_header "/Nginx-Fancyindex-Theme/header.html";
fancyindex_footer "/Nginx-Fancyindex-Theme/footer.html";

# Ignored files will not show up in the directory listing, but will still be public.
fancyindex_ignore "examplefile.html";

# Making sure folder where files are don't show up in the listing.
fancyindex_ignore "Nginx-Fancyindex-Theme";

# Maximum file name length in bytes, change as you like.
fancyindex_name_length 255;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM