nginx搭建静态文件服务器,Nginx 静态文件服务器搭建及autoindex模块解析
導讀
文章重點講述nginx靜態服務器搭建
ngx_http_autoindex_module
ngx_http_autoindex_module模塊處理以斜杠字符('/')結尾的請求,并生成目錄列表。當
ngx_http_index_module模塊找不到index.html文件時,通常會將請求傳遞給
ngx_http_autoindex_module模塊。
配置樣例:
location?/?{
root?/home/map/www/;?#指定目錄所在路徑
autoindex?on;?#?打開目錄瀏覽功能
autoindex_exact_size?off;#?以可讀的方式顯示文件大小,單位為?KB、MB?或者?GB,autoindex_format為html格式時有效
autoindex_localtime?on;?#?以服務器的文件時間作為顯示的時間,autoindex_format為html格式時有效
#autoindex_format?html;?#?以網頁的風格展示目錄內容。該屬性在1.7.9及以上適用
charset?utf-8,gbk;?#?展示中文文件名
}
autoindex
開啟或者關閉列出目錄中文件的功能
Syntax:?autoindex?on?|?off;
Default:?autoindex?off;
Context:?http,?server,?location
autoindex_exact_size
對于HTML格式,指定是否應在目錄列表中輸出精確的文件大小,或者將其舍入為千字節,兆字節和千兆字節
Syntax:?autoindex_exact_size?on?|?off;
Default:?autoindex_exact_size?on;
Context:?http,?server,?location
autoindex_localtime
對于HTML格式,指定是否應該在本地時區或UTC中輸出目錄列表中的時間。
Syntax:?autoindex_localtime?on?|?off;
Default:?autoindex_localtime?off;
Context:?http,?server,?location
autoindex_format
設置目錄列表的格式。
當使用JSONP格式時,使用callback請求參數設置回調函數的名稱。如果參數丟失或具有空值,則使用JSON格式。
XML輸出可以使用ngx_http_xslt_module模塊進行轉換。
Syntax:?autoindex_format?html?|?xml?|?json?|?jsonp;
Default:?autoindex_format?html;
Context:?http,?server,?location
This?directive?appeared?in?version?1.7.9.
Nginx Fancy Index module
在實際體驗中
ngx_http_autoindex_module模塊顯示出的界面有點不好看,所以我們可以采用第三的
Nginx Fancy Index module模塊,該模塊提供動態加載,當然也可以編譯,這里采用編譯安裝,替換原有的nginx二進制文件。
如果是動態加載模塊需要在events塊上面添加:
load_module "modules/ngx_http_fancyindex_module.so";
#?查看原有的nginx編譯模塊
[root@localhost?sbin]#?./nginx?-V
nginx?version:?nginx/1.14.2
built?by?gcc?4.8.5?20150623?(Red?Hat?4.8.5-39)?(GCC)
configure?arguments:?--prefix=/usr/local/nginx
下載Nginx?Fancy?Index?module?源碼包:
#?編譯
./configure?--prefix=/usr/local/nginx?--add-module=/root/nginx-src/ngx-fancyindex-0.4.4
make
#不能make?insatll
備份原本的nginx二進制文件,用新生成的二進制文件做替換,在nginx.conf中添加一下內容,?fancyindex就已經啟用完成
編輯nginx.conf
location?/?{
#include?/usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf;
root?django-document/;
#autoindex?on;
#autoindex_exact_size?off;
fancyindex?on;?#開啟nginx目錄瀏覽功能
fancyindex_exact_size?off;?#文件大小從KB開始顯示
fancyindex_localtime?on;?#顯示文件修改時間為服務器本地時間
#set?$limit_rate?1k;
#root?html;
#index?index.html?index.htm;
}
此時主題還是略丑,可以下載主題包
git clone
復制
Nginx-Fancyindex-Theme文件到root 文件夾下,在nginx.conf中引入主題包,美化完畢
location?/?{
include?/usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf;
root?django-document/;
autoindex?on;
autoindex_exact_size?off;
}
本文原創地址:
總結
以上是生活随笔為你收集整理的nginx搭建静态文件服务器,Nginx 静态文件服务器搭建及autoindex模块解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 古代树森林雌火龙在哪(古代的各种称谓)
- 下一篇: ajax添加一行,ajax请求到数据会给