Nginx 目录浏览基础与进阶

摘要:
目录1,描述2,配置目录浏览3,高级配置3.1添加第三方模块3.2修改配置文件3.3重新启动测试3.4自定义主题1,描述Nginx作为一个优秀的web服务器,默认情况下不允许列出站点的整个目录。如果需要配置,则需要单独打开此功能。此功能通常用于为intranet功能(如下载文件)打开单独的虚拟主机。在其他情况下,出于安全考虑,通常不会启用此功能。2.配置目录浏览服务器{listen80;indexindex.htmlind

目录

1、简述

Nginx作为一款优秀的web服务器,其默认不允许列出站点的整个目录,如果需要配置,需要单独打开此功能

此功能一般用于单独开设虚拟主机供内网如下载文件等功能使用,其他情况下为了安全,一般不会开启此功能

2、配置目录浏览
server {
   listen       80;
   index index.html index.htm;
   server_name downloads.ssgeek.com;
   root /data/downloads;
   autoindex on;  # 开启目录浏览功能
   autoindex_localtime on;  # 显示本地时间
   autoindex_format html;  # 输入格式,可选项为html、xml、json、jsonp
   autoindex_exact_size off;  # 显示精确字节大小还是显示友好可读的大小
   charset utf-8,gbk;  # 保证以中文命名的文件显示不会乱码
}

除上述配置之外,还可以利用basic_auth为目录浏览功能,加上认证功能以增加一定的安全性

配置完成后重启Nginx,效果如下

Nginx 目录浏览基础与进阶第1张3、进阶版配置

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

3.1 添加第三方模块

查看现有nginx版本及编译参数

# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2l  25 May 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-pcre=/usr/local/pcre-8.39 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.2l --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --http-log-path=/usr/local/nginx/log/access.log --pid-path=/usr/local/nginx/run/nginx.pid

下载第三方模块源码并解压

# wget -c https://github.com/aperezdc/ngx-fancyindex/archive/v0.5.1.tar.gz
# tar xf v0.5.1.tar.gz

在已经安装的nginx下添加新的第三方模块

# pwd
/root
# cd nginx-1.14.2/
# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-pcre=/usr/local/pcre-8.39 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.2l --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --http-log-path=/usr/local/nginx/log/access.log --pid-path=/usr/local/nginx/run/nginx.pid --add-module=/root/ngx-fancyindex-0.5.1
# make # 只编译不安装
# /usr/local/nginx/sbin/nginx -s stop
# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# cp objs/nginx /usr/local/nginx/sbin/
# chown www:www /usr/local/nginx/sbin/nginx

3.2 修改配置文件

配置文件的详细配置参考如下

server {
   listen       80;
   index index.html index.htm;
   server_name downloads.ssgeek.com;
   charset utf-8,gbk;
   location / {
       root /data/downloads;
       # 防止浏览器预览打开
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;  # 开启第三方模块的目录浏览
    #    fancyindex_default_sort name;  # 排序规则,默认name,可选项name、size、date、name_desc、size_des、date_desc
    #    fancyindex_directories_first on; # 是否将目录分组在一起并在所有常规文件之前对它们进行排序,默认启用
    #    fancyindex_css_href "";  # 插入指向CSS样式表的链接
       fancyindex_exact_size off;  # 显示精确字节大小还是显示友好可读的大小
       fancyindex_name_length 500;  # 定义最大文件名长度限制(以字节为单位)
    #    fancyindex_footer "";  # 定义在目录列表的底部插入哪个文件
    #    fancyindex_header "";  # 定义在目录列表的顶部插入哪个文件
    #    fancyindex_show_path on;  # 在标题之后是否输出路径和结束</ h1>标记,默认启用
    #    fancyindex_show_dotfiles on;  # 是否列出以点开头的文件,默认关闭
    #    fancyindex_ignore "";  # 指定不显示的文件名列表
    #    fancyindex_hide_symlinks off;  # 是否隐藏符号链接,默认关闭
    #    fancyindex_hide_parent_dir on;  # 是否隐藏父目录,默认关闭
       fancyindex_localtime on;  # 时间显示为本地时间,默认关闭,显示为格林尼治标准时间
       fancyindex_time_format "%Y-%b-%d %H:%M";  # 显示的时间格式,默认为%Y-%b-%d %H:%M
   }
}

这里我的配置如下

   location / {
       root /data/downloads;
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;
       fancyindex_exact_size off;
       fancyindex_name_length 500;
       fancyindex_show_dotfiles on;
       fancyindex_localtime on;
       fancyindex_time_format "%Y-%m-%d %H:%M:%S";
   }

3.3 重启测试

修改完配置文件后,重启进行测试,效果如下

Nginx 目录浏览基础与进阶第2张

3.4 自定义主题

如果觉得上面的还不是太好看,项目中也提供了更多主题供配置,主题的地址如下

主题一:使用自定义的页眉和页脚

主题二:使用自定义页眉和页脚,页眉包含搜索字段,可使用JavaScript按文件名过滤

主题三:使用Material Design元素的响应主题

主题四:基于Bootstrap 4和FontAwesome的简单扁平主题

四个主题的配置和效果分别如下

  • 主题一

下载主题相关样式代码,目录结构如下

# tree -L 1 /data/downloads/fancyindex
/data/downloads/fancyindex
├── css
├── fonts
├── footer.html
├── header.html
├── icons
├── images
└── js

配置文件

...
   location / {
       /data/downloads;
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;
       fancyindex_exact_size off;
       fancyindex_name_length 500;
       fancyindex_show_dotfiles on;
       fancyindex_ignore "fancyindex";
       fancyindex_header "/fancyindex/header.html";
       fancyindex_footer "/fancyindex/footer.html";
       fancyindex_localtime on;
       fancyindex_time_format "%Y-%m-%d %H:%M:%S";
   }
...

效果如下

Nginx 目录浏览基础与进阶第3张
  • 主题二

下载主题相关样式代码,目录结构如下

# # tree -L 1 /data/downloads/Nginx-Fancyindex-Theme-dark
/data/downloads/Nginx-Fancyindex-Theme-dark
├── addNginxFancyIndexForm.js
├── footer.html
├── header.html
├── jquery.min.js
├── showdown.min.js
└── styles.css

配置文件

...
   location / {
       root /data/downloads;
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;
       fancyindex_exact_size off;
       fancyindex_name_length 500;
       fancyindex_show_dotfiles on;
       fancyindex_ignore "Nginx-Fancyindex-Theme-dark";
       fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
       fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
       fancyindex_localtime on;
       fancyindex_time_format "%Y-%m-%d %H:%M:%S";
   }
...

效果如下

Nginx 目录浏览基础与进阶第4张
  • 主题三

下载主题相关样式代码,目录结构如下

# tree -L 1 /data/downloads/fancyindex
/data/downloads/fancyindex
├── clipboard.min.js
├── dialog-polyfill.css
├── dialog-polyfill.js
├── fancyindex.css
├── fancyindex.js
├── font
├── footer.html
├── header.html
├── images
├── material-icons.css
├── mdl
├── README.md
└── roboto.css

配置文件同主题一

...
   location / {
       root /data/downloads;
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;
       fancyindex_exact_size off;
       fancyindex_name_length 500;
       fancyindex_show_dotfiles on;
       fancyindex_ignore "fancyindex";
       fancyindex_header "/fancyindex/header.html";
       fancyindex_footer "/fancyindex/footer.html";
       fancyindex_localtime on;
       fancyindex_time_format "%Y-%m-%d %H:%M:%S";
   }
...

效果如下

Nginx 目录浏览基础与进阶第5张
  • 主题四

下载主题相关样式代码,目录结构如下

# tree -L 1 /data/downloads/theme
/data/downloads/theme
├── footer.html
├── header.html
├── js
├── less
├── LICENSE
├── Makefile
├── README.md
└── theme.less

配置文件

...
   location / {
       root /data/downloads;
       if ($request_filename ~* ^.*?.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
           add_header Content-Disposition attachment;
       }
       fancyindex on;
       fancyindex_exact_size off;
       fancyindex_name_length 500;
       fancyindex_show_dotfiles on;
       fancyindex_ignore "theme";
       fancyindex_header "/theme/header.html";
       fancyindex_footer "/theme/footer.html";
       fancyindex_localtime on;
       fancyindex_time_format "%Y-%m-%d %H:%M:%S";
   }
...

效果如下

Nginx 目录浏览基础与进阶第6张

免责声明:文章转载自《Nginx 目录浏览基础与进阶》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android之文件操作——读取assets和raw文件下的内容WPF等待窗口下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

AG-Admin微服务框架入门

  AG-Admin微服务框架入门  @qq群:一群: 837736451  二群 169824183 一 概要介绍 AG-Admin后台地址:https://gitee.com/minull/ace-security AG-Admin前端地址:https://gitee.com/minull/AG-Admin-v2.0 要想玩儿转spring cloud...

nginx利用fastcgi_cache模块缓存

nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache。proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。proxy_cache缓存减少了nginx与后端通信的...

Nginx线上部署多个Vue项目(路径区分)

Nginx线上部署多个Vue项目(路径区分) nginx 多静态文件部署。 我个人博客项目占用了nginx根路径,而我只有一个域名,也不准备用二级域名了, 所以就直接用路径区分开。 一个域名,只部署一个静态文件服务,那很简单,只用把打包后文件放上去就行。 多个Vue项目主要就是路径问题,静态js文件以及自定义的路由。 原文请访问 我个人博客地址 博客地址:...

分析nginx日志常用的命令总结

1. 利用grep ,wc命令统计某个请求或字符串出现的次数 比如统计GET /app/kevinContent接口在某天的调用次数,则可以使用如下命令: cat /usr/local/nginx/logs/access.log | grep 'GET /app/kevinContent' | wc -l 其中cat用来读取日志内容,grep进行匹配的文...

Nginx的高级使用

1、概述 之前介绍过Nginx的简单使用,今天来聊聊Nginx的一些高级使用。 2、使用Nginx解决跨域问题 当公司存在多个域名时,两个不同的域名相互访问就会存在跨域问题。 或者在进行前端开发时,通常前端代码在本地启动,而后端代码会部署在一台专用的后端开发服务器上,此时前端去调用后端接口时,就会出现跨域问题。 解决跨域的方法有很多,今天来说一下如何使用N...

1.初识nginx

​在一个遥远的乡村发生了一件事,村里的邮差老王要退休,同时宣布小张成为新一任的邮差,继续为村民服务。这里的村民生活的nginx帝国地域辽阔,人口众多。帝国和百姓沟通的媒介就是各村的邮差。村民平时遇到问题只需要交给邮差,国王就会迅速将处理 a结果交由邮差带回。 这天村民李二将一张名为”https://xxx.com/abc?name=li“ 的凭条交给了小张...