共计 1887 个字符,预计需要花费 5 分钟才能阅读完成。
一、开启目录浏览功能
Nginx 提供了相应的 ngx_http_autoindex_module 模块
该模块有以下几个命令:
| 命令 | 默认值 | 值域 | 作用域 | 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时有效 |
二、浏览目录基本配置
根据上面的命令,一个简单的Nginx浏览目录的配置如下:
location /{
root /www/wwwroot/cdn.iox7.com/; #指定目录所在路径
autoindex on; #开启目录浏览
autoindex_format html; #以html风格将目录展示在浏览器中
autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_localtime on; #以服务器的文件时间作为显示的时间
charset utf-8,gbk; #展示中文文件名,防止乱码
}
三、目录浏览美化
我们使用开源的Fancy Index来美化页面:Github
在美化之前,需要安装Nginx FancyIndex模块。安装模块步骤如下。
1、查看完整编译参数
查看完整的编译参数 nginx -V 如下:
nginx version: nginx/1.13.8
built by clang 9.0.0 (clang-900.0.39.2)
built with OpenSSL 1.1.0f 25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --sbin-path=/usr/local/nginx/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl@1.1/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl@1.1/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module --with-http_v2_module
正文完