https://segmentfault.com/a/1190000012606305

在项目中有一个功能需要在浏览器页面中浏览服务器的目录。服务器使用Nginx,而Nginx提供了相应的ngx_http_autoindex_module 模块,该模块提供了我们想要的功能。

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浏览目录的配置如下:

  1. location /download
  2. {
  3. root /home/map/www/; #指定目录所在路径
  4. autoindex on; #开启目录浏览
  5. autoindex_format html; #以html风格将目录展示在浏览器中
  6. autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
  7. autoindex_localtime on; #以服务器的文件时间作为显示的时间
  8. }

页面展示如下:

可以看到页面中的展示信息和配置想要的一致,但还有个问题是中文文件名显示的时候乱码。

中文文件名乱码

要解决上面的问题,只需要添加如下配置即可:

  1. charset utf-8,gbk; #展示中文文件名

完整配置如下:

  1. location /download
  2. {
  3. root /home/map/www/; #指定目录所在路径
  4. autoindex on; #开启目录浏览
  5. autoindex_format html; #以html风格将目录展示在浏览器中
  6. autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
  7. autoindex_localtime on; #以服务器的文件时间作为显示的时间
  8. charset utf-8,gbk; #展示中文文件名
  9. }

页面展示如下:

文件列表的第一行是一个目录,点进去,展示如下:

稍微有一点审美的同学是不是觉得这样展示不太美观呢?是的,很不美观,感觉乱糟糟的。下面就来解决这个问题。

目录浏览美化

我们使用开源的Fancy Index来美化页面,Github看这里

在美化之前,需要安装Nginx FancyIndex模块。安装模块步骤如下。

查看Nginx当前编译了哪些模块

要查看Nginx编译了哪些模块,执行以下命令:2>&1 nginx -V | tr ' ' '\n'|grep module,如下:

查看完整的编译参数:nginx -V,如下:

内容如下:

  1. nginx version: nginx/1.13.8
  2. built by clang 9.0.0 (clang-900.0.39.2)
  3. built with OpenSSL 1.1.0f 25 May 2017
  4. TLS SNI support enabled
  5. 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

动态编译添加Nginx模块

  1. 在GitHub下载最新源码:ngx-fancyindex
  2. 源码下载下来后,解压,放到nginx源码目录(/usr/local/nginx)中,执行下面的代码,编译:

    ./configure --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 --add-module=ngx-fancyindex-0.4.2

  3. make <font color="red">这里不要make install!!!</font>
  4. 进入nginx源码目录下的objs目录,执行2>&1 ./nginx -V | tr ' ' '\n'|grep fan
  5. objs目录下的nginx文件替换/usr/bin下面的nginx即可

选择Fancy Index主题

在Github里面找到了两个开源的主题,分别是:

大家选一个自己喜欢的就好了,这里我选的是第一个。

但是在实际使用过程中,第一个代码有一些问题,我做了一些修改,想要直接可以使用的,可以用这个:https://github.com/lanffy/Nginx-Fancyindex-Theme

Fancy Index 配置

  1. 进入Nginx安装的web目录,执行nginx -V,输出configure arguments: --prefix=/usr/local/nginx,就是这个目录
  2. git clone https://github.com/lanffy/Nginx-Fancyindex-Theme.git
  3. 在nginx location模块中添加Fancy Index配置,如下:

    location /download
    {

    1. include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf; # 目录美化配置
    2. root /home/map/www/; #指定目录所在路径
    3. autoindex on; #开启目录浏览
    4. autoindex_format html; #以html风格将目录展示在浏览器中
    5. autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    6. autoindex_localtime on; #以服务器的文件时间作为显示的时间
    7. charset utf-8,gbk; #展示中文文件名

    }

  4. 重启Nginx即可

到这一步就完成配置了,最终页面展示如下:

该主题有两种风格,上面一种是light风格,下面的是dark风格:

风格在/usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf;配置文件中进行修改。

Nginx浏览目录配置及美化的更多相关文章

  1. nginx 静态目录配置规则,路径匹配与本地资源

    经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使 用以下规则 假如nginx是在本机,静态目录也是在本机, 1.子目录匹配 如下配置 location ...

  2. nginx 静态目录配置规则

    1.子目录匹配 如下配置 location / { root /data/www; } 访问http://127.0.0.1/时,配匹配/data/www 访问http://127.0.0.1/ima ...

  3. nginx索引目录配置

    为了简单共享文件,有些人使用svn,有些人使用ftp,但是更多得人使用索引(index)功能.apache得索引功能强大,并且也是最常见得,nginx得auto_index实现得目录索引偏少,而且功能 ...

  4. nginx浏览目录

    [root@localhost domains]# vi web.jd.com location / proxy_set_header X-Forwarded-For $proxy_add_x_for ...

  5. nginx虚拟目录配置

    参考文章:https://blog.csdn.net/whatday/article/details/50649461 1. location ~ ^/awstats/ { root /home/aw ...

  6. Nginx笔记总结二十:nginx索引目录配置

    location / { autoindex on; autoindex_localtime on; }

  7. Nginx 安装及配置

    目录 概念 安装 配置文件 主要文件位置 注意点 Nginx运行 FAQ Q1:nginx: [error] open() "/usr/local/var/run/nginx.pid&quo ...

  8. Nginx 虚拟目录和虚拟主机的配置

    nginx.conf 配置文件的几个常用命令 nginx 配置文件主要分为六个区域: main: 全局设置 events: nginx工作模式 http: http设置 sever: 主机设置 loc ...

  9. nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet

    nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet Nginx custom autoindex with XSLT 转载注明来源: 本文链接 来自osnosn ...

随机推荐

  1. mybatis-generator没有自动生成代码和Junit测试controller

    本来mybatis的generator想要自动生成增删改的,但是到后来语句就两个select,原因是数据中没有给字段加primary,就不会有删改增. 以及Controller的Junit测试 先导入 ...

  2. api中locale或language字段,传送客户端地域信息,一般为下划线

    在请求新闻的分类信息和新闻内容时,需要在api地址中传入local参数,根据用户地区不同返回不同的新闻和分类. local参数,通过navigator.languages[0]获取, 但是,问题来了: ...

  3. css 伪类: 1)a:link , a:visited, a:hover, a:active 2):first-child

    1. 链接伪类: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...

  4. WebService的一种简单应用方式入门

    1.什么是WebService? WebService即Web服务,简单来讲,他就是一种跨编程语言和跨操作平台的远程调用技术. 2.Web服务: Web服务是基于HTTP和XML的技术:HTTP是互联 ...

  5. 创建自动化环境(jenkins+tomcat+git+maven,java)

    1.安装jdk 下载1.8以上jdk // 切换到lib cd /usr/lib sudo mkdir jdk cd jdk // 将 jdk拷贝到此目录 // 解压jdk tar -zxvf jdk ...

  6. wx小程序 使用字体

    1.下载项目下的字体库 2.解压复制iconfont.css中的代码到,小程序 app.wxss 3.使用: //icon-begindate表示开始时间的图标 <text class=&quo ...

  7. HDU6043 17多校1 KazaQ's Socks 水题

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6043 Problem Description KazaQ wears socks everyday. ...

  8. 2018最新APP Android UI设计规范

    设计稿尺寸:从目前市场主流设备尺寸来看,我们要用 1080 x 1920 PX  来做安卓设计稿尺寸. 以1080x1920px作为设计稿标准尺寸的原由: 从中间尺寸向上和向下适配的时候界面调整的幅度 ...

  9. 实验楼 Linux 基础入门(新版)挑战:寻找文件

    传送门:https://www.shiyanlou.com/courses/running 挑战:寻找文件 实验环境: 用户名:shiyanlou 密码:76036575 寻找文件 介绍 有一个非常重 ...

  10. 使用 TortoiseSVN 创建 svn branch

    1.使用TortoiseSVN->Repo-browser进入仓库. 2.选择需要创建分支的文件->Copy to 添加分支路径后,点击ok Rename:trunk路径 格式:https ...