Nginx服务配置 (四) 文件列表显示配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feit2417/article/details/85002166

当用户访问的站点或者目录没有index指令设置默认的索引文件(比如index.html)的时候,就会报403错误。当开启了目录列表功能后,再出现找不到index索引文件的情况就会以列表形式展示。  

                                                

目的:本文重点配置nginx根目录通过列表显示给浏览器,并设置显示文件的大小与时间格式

开启目录列表功能

通过autoindex指令,该指令在不同位置的作用范围不同。在http块中表示对所有站点有效;在server块中表示对该站点有效;在location块中表示对某个目录有效。
1.编写子配置文件

[root@VM_16_8_centos conf.d]# vim /etc/nginx/conf.d/virtual.conf

server {
        listen 80;
        server_name www.benmoom.club;
        root html/benmoom;
        index index.htm;
        autoindex on;
}

2.重启nginx服务

[root@VM_16_8_centos conf.d]# systemctl restart nginx

3.添加一个测试文件

[root@VM_16_8_centos conf.d]# cd /usr/share/nginx/html/benmoom
[root@VM_16_8_centos benmoom]# dd if=/dev/zero of=./test.a bs=8M count=1
1+0 records in
1+0 records out
8388608 bytes (8.4 MB) copied, 0.00801504 s, 1.0 GB/s
[root@VM_16_8_centos benmoom]# du -h test.a
8.0M    test.a

4.访问域名查看结果
根据配置文件,root目录下没有index.htm这个索引文件,又设置了目录列表功能,所以展示出来的就是该目录下的所有文件了

设置文件的时间格式和大小

  • 通过autoindex_exact_size指令设置是否精准显示文件的大小,默认值 on
  • 通过autoindex_exact_size指令设置最后一次时间的更改模式,默认值 off

1.文件配置

[root@VM_16_8_centos conf.d]# vim /etc/nginx/conf.d/virtual.conf

server {
        listen 80;
        server_name www.benmoom.club;
        root html/benmoom;
        index index.php index.htm;
        autoindex on;
        autoindex_exact_size off;
        autoindex_exact_size on;
}

2.重启nginx服务

[root@VM_16_8_centos conf.d]# systemctl restart nginx

3.添加一个测试文件

[root@VM_16_8_centos conf.d]# cd /usr/share/nginx/html/benmoom
[root@VM_16_8_centos benmoom]# dd if=/dev/zero of=./test.a bs=8M count=1
1+0 records in
1+0 records out
8388608 bytes (8.4 MB) copied, 0.00801504 s, 1.0 GB/s
[root@VM_16_8_centos benmoom]# du -h test.a
8.0M    test.a

4.访问域名查看结果
根据配置文件,root目录下没有index.htm这个索引文件,又设置了目录列表功能,所以展示出来的就是该目录下的所有文件了;这里设置了时间和文件大小的显示,显示文件大概大小,时间为文件的服务器时间。

猜你喜欢

转载自blog.csdn.net/feit2417/article/details/85002166
今日推荐