1

Nginx目录浏览并进行美化

 1 year ago
source link: https://qq52o.me/2816.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
nginx-logo.png

Nginx 作为一个高性能的 HTTP 和反向代理 web 服务器,默认不允许列出站点的整个目录,如果需要开启,可以通过配置打开此功能。

此功能一般作为在内网中提供下载文件等功能使用。

配置目录浏览

server {
    listen       80;
    index index.html index.htm;
    server_name dl.qq52o.me;
    root /home/lufei/downloads;
    autoindex on;  # 开启目录浏览功能
    autoindex_localtime on;  # 显示本地时间
    autoindex_format html;  # 输入格式,可选项为html、xml、json、jsonp
    autoindex_exact_size off;  # 显示精确字节大小还是显示友好可读的大小
}

配置完成后重启 Nginx,访问配置好的 server_name 即可看到效果,效果如下:

美化目录浏览

Nginx 自带的目录浏览功能看起来并不是那么美观,可以使用第三方模块 ngx-fancyindex 插件来美化目录浏览功能。

我使用的 ubuntu,并且 Nginx 也是通过 apt 安装的

sudo apt install libnginx-mod-http-fancyindex

所以我这里使用了此命令进行了安装,其他系统可以参考 README 进行安装。

安装完成后,启用 fancyindex,修改刚才的配置文件

server {
    listen       80;
    index index.html index.htm;
    server_name dl.qq52o.me;
    root /home/lufei/downloads;

    fancyindex on;            # 开启索引功能
    fancyindex_exact_size off; # 关闭文件大小
    fancyindex_localtime on; #开启时间显示
    fancyindex_name_length 255; #最大名字长度
    fancyindex_time_format "%Y-%m-%d %H:%M:%S";
}

重启以后就可以看到效果。

我选择了一个使用 Material Design 元素的响应主题。主题地址:https://github.com/fraoustin/Nginx-Fancyindex-Theme

拉取主题代码,需要放在启用目录浏览的目录下:

cd /home/lufei/downloads
git clone https://github.com/fraoustin/Nginx-Fancyindex-Theme.git fancyindex

然后修改配置,加上主题配置:

fancyindex_header "/fancyindex/header.html";
fancyindex_footer "/fancyindex/footer.html";
fancyindex_ignore "fancyindex";

再次重启 Nginx,就可以看到最终的效果了:

任何个人或团体,未经允许禁止转载本文:《Nginx 目录浏览并进行美化》,谢谢合作!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK