為了安全起見,Nginx默認是不允許列出整個目錄的,即當訪問一個不包含首頁的目錄時會返回 403 錯誤,當我們需要將服務器某一目錄列出索引以便下載,我們可以使用 autoindex 來實現,但是 autoindex 模塊生成的索引非常簡陋,我們可以使用 ngx-fancyindex 代替 autoindex 實現索引目錄美化。
本文為已安裝 Nginx 環(huán)境下的操作。
安裝ngx-fancyindex
第一步
查看已安裝的 Nginx 版本與模塊信息:
輸出信息類似于:
nginx version: nginx/1.8.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
第二步
下載對應版本的 Nginx 源碼包: http://nginx.org/download/
下載最新版本的 ngx-fancyindex 源碼包: https://github.com/aperezdc/ngx-fancyindex/releases
上傳至服務器并解壓,這里我們上傳到/tmp目錄。
第三步
編譯 Nginx
cd /tmp/nginx-1.8.0 #進入源碼目錄
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx --add-module=../ngx-fancyindex-0.4.2
make #編譯
特別注意:
- ./configure后面的配置要對應第一步的輸出信息,防止不必要的麻煩,–add-module=../ngx-fancyindex-0.4.2 這里根據你下載解壓后的 ngx-fancyindex 目錄名來。
- 只需要 make,不需要 install.
第四步
重命名舊的 nginx 文件:
mv /usr/local/nginx /usr/local/nginx.bak
復制重新編譯的nginx文件到nginx原來安裝目錄下:
cp ./objs/nginx /usr/local/nginx/sbin/
重新啟動 nginx 服務:
配置ngx-fancyindex
修改nginx配置文件
location /path/ #指定~/path目錄開啟自動列目錄
{
alias /alliot/path/; #虛擬目錄/alliot/path/開啟自動列目錄
root /path/; #實際目錄/path/開啟自動列目錄 與alias二選一
fancyindex on; #開啟nginx目錄瀏覽功能
fancyindex_exact_size off; #文件大小從KB開始顯示
fancyindex_localtime on; #顯示文件修改時間為服務器本地時間
fancyindex_footer "footer.html"; #設置footer為當前目錄下的footer.html
fancyindex_ignore "footer.html"; #設置不列出當前目錄下的footer.html
}
上面 alias 與 root 的區(qū)別在于, alias 指定的是當前目錄,而 root 指定的是根目錄,一般情況下,建議在 “l(fā)ocation /” 中通過root命令來配置根目錄。
更多配置見 https://github.com/aperezdc/ngx-fancyindex
重載配置
執(zhí)行
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。