nginx列出整个目录文件列表

in web with 0 comment

今天测试下载服务器。生产环境是Nginx做的前端。于是我先在自己的虚拟机中安装测试了一下,直接编译安装了nginx。安装过程这里就不说了,比较简单。

说重点,安装完成后,修改配置文件,此处我把我的配置文件给出:

server {
                listen   81;
                server_name  www.ceshi.com;
                root   /data;
                access_log  /usr/local/nginx/logs/access.log;
                error_log   /usr/local/nginx/logs/error.log;
}

于是在windows本地写好hosts文件

添加了解析记录:192.168.199.131  www.ceshi.com

[root@pxebackup nginx]# /usr/local/nginx/sbin/nginx -t    #检查配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@pxebackup nginx]# /usr/local/nginx/sbin/nginx    #启动服务
[root@pxebackup vhost]# ps aux| grep nginx
root     50778  0.0  0.1  20288  1284 ?        Ss   16:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   50826  0.0  0.3  22732  3672 ?        S    17:26   0:00 nginx: worker process      
root     50835  0.0  0.0 103248   876 pts/1    S+   17:37   0:00 grep nginx
[root@pxebackup vhost]# netstat -alntp|grep 81
tcp        0      0 0.0.0.0:81                  0.0.0.0:*                   LISTEN      50778/nginx  
#以上都执行完,确认进程启动。

在浏览器中测试:

1

查看访问日志:

[root@pxebackup logs]# tail access.log 
192.168.199.1 - - [28/Dec/2015:16:59:57 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:16:59:57 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:16:59:57 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:16:59:58 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:16:59:58 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:16:59:58 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:17:00:00 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:17:12:33 +0800] "GET / HTTP/1.1" 403 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
192.168.199.1 - - [28/Dec/2015:17:21:34 +0800] "GET /index.html HTTP/1.1" 200 6 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"

再看错误日志:

[root@pxebackup logs]# tail error.log 
2015/12/28 16:57:41 [error] 50779#0: *5 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:56 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:57 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:57 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:57 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:58 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:58 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 16:59:58 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 17:00:00 [error] 50779#0: *8 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"
2015/12/28 17:12:33 [error] 50779#0: *9 directory index of "/data/" is forbidden, client: 192.168.199.1, server: www.ceshi.com, request: "GET / HTTP/1.1", host: "www.ceshi.com:81"

然后就百度、google未果,自己反复检查web目录权限等,都没有头绪,于是把问题反馈到技术群里的基友们,提出的几个方法都未能找到问题,终于最后一位小伙伴觉得是配置文件的问题,叫我添加了几个参数,测试之后果断成功显示了网站的文件列表。修改后的配置文件如下:

server {
                listen   81;
                server_name  www.ceshi.com;
                root   /data;
                access_log  /usr/local/nginx/logs/access.log;
                error_log   /usr/local/nginx/logs/error.log;
                autoindex on;
                autoindex_localtime on;
                autoindex_exact_size on;
}

原来nginx默认是不允许列出整个目录的,看到这想起了之前配置的Apache,默认确是显示整个目录内容的,不禁解除了疑惑啊。

添加如上配置文件中的三个字段。可以开启Nginx目录文件列表功能

auoindex on;

下面两参数最好也一起加上。

autoindex_exact_size on;      #显示出文件的确切大小,单位是byte.改为off后,显示出文件的大概大小,单位是KB或者GB.

autoindex_localtime on;        #默认为off,显示的时间为GMT时间,改为on,则显示的文件时间为文件的服务器时间.

浏览器显示如下:

2

 

感谢基友们的帮助得以解决了这个问题。希望遇到同样问题的朋友可以有个参照。

 

Responses