Nginx默认不开放目录浏览,我们可以修改 nginx 配置文件来启用它的目录浏览功能。

修改配置文件:

1、vim /usr/local/nginx/conf/nginx.conf

在server项下面加入:

  1. location /share { #share 必须是网站下面定义的一个目录,可以自行定义添加
    autoindex on; #开启目录浏览功能
    autoindex_exact_size off;
    autoindex_localtime on;
    }

修改后完整的代码为:

server {
        location /share{
        charset utf-8,gbk;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
}
        listen       80;
        server_name  www.chenjiahe.com;
        root         html;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm index.php;
        }

autoindex_exact_size off;  默认为on,显示出文件的确切大小,单位是bytes;改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;     默认为off,显示的文件时间为GMT时间; 改为on后,显示的文件时间为文件的服务器时间


修改文件后,保存退出,重启nginx 服务:

service nginx restart


输入nginx serverIP/share,浏览开启的目录:


wKiom1bn_zTytX5mAABSxELaEsE573.png

接下来,我们就可以在服务器后端上传文件供人下载了。

有时候会遇到中文乱码情况,在server 配置中记得加上

charset utf-8,gbk;

设置完后还有可能是浏览器问题,兼容性设置里调整一下就OK了。