Nginx服務配置 (四) 檔案列表顯示配置
阿新 • • 發佈:2018-12-15
當用戶訪問的站點或者目錄沒有index指令設定預設的索引檔案(比如index.html)的時候,就會報403錯誤。當開啟了目錄列表功能後,再出現找不到index索引檔案的情況就會以列表形式展示。
目的:本文重點配置nginx根目錄通過列表顯示給瀏覽器,並設定顯示檔案的大小與時間格式
開啟目錄列表功能
通過autoindex指令,該指令在不同位置的作用範圍不同。在http塊中表示對所有站點有效;在server塊中表示對該站點有效;在location塊中表示對某個目錄有效。
1.編寫子配置檔案
[[email protected]_16_8_centos conf.d]# vim /etc/nginx/conf.d/virtual.conf server { listen 80; server_name www.benmoom.club; root html/benmoom; index index.htm; autoindex on; }
2.重啟nginx服務
[[email protected]_16_8_centos conf.d]# systemctl restart nginx
3.新增一個測試檔案
[[email protected]_16_8_centos conf.d]# cd /usr/share/nginx/html/benmoom
[[email protected]_16_8_centos benmoom]# dd if=/dev/zero of=./test.a bs=8M count=1
1+0 records in
1+0 records out
8388608 bytes (8.4 MB) copied, 0.00801504 s, 1.0 GB/s
[ [email protected]_16_8_centos benmoom]# du -h test.a
8.0M test.a
4.訪問域名檢視結果
根據配置檔案,root目錄下沒有index.htm這個索引檔案,又設定了目錄列表功能,所以展示出來的就是該目錄下的所有檔案了
設定檔案的時間格式和大小
- 通過autoindex_exact_size指令設定是否精準顯示檔案的大小,預設值 on
- 通過autoindex_exact_size指令設定最後一次時間的更改模式,預設值 off
1.檔案配置
[[email protected]_16_8_centos conf.d]# vim /etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name www.benmoom.club;
root html/benmoom;
index index.php index.htm;
autoindex on;
autoindex_exact_size off;
autoindex_exact_size on;
}
2.重啟nginx服務
[[email protected]_16_8_centos conf.d]# systemctl restart nginx
3.新增一個測試檔案
[[email protected]_16_8_centos conf.d]# cd /usr/share/nginx/html/benmoom
[[email protected]_16_8_centos benmoom]# dd if=/dev/zero of=./test.a bs=8M count=1
1+0 records in
1+0 records out
8388608 bytes (8.4 MB) copied, 0.00801504 s, 1.0 GB/s
[[email protected]_16_8_centos benmoom]# du -h test.a
8.0M test.a
4.訪問域名檢視結果
根據配置檔案,root目錄下沒有index.htm這個索引檔案,又設定了目錄列表功能,所以展示出來的就是該目錄下的所有檔案了;這裡設定了時間和檔案大小的顯示,顯示檔案大概大小,時間為檔案的伺服器時間。