nginx显示文件目录列表并提供文件下载服务

测试环境:

centos 7 64 位

安装并启动nginx

# yum -y install wget vim
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum clean all
# yum -y install nginx
# iptables -F
# service nginx start

更改nginx配置文件

# cd /etc/nginx/
# cp nginx.conf nginx.conf.bk
# vim nginx.conf   <--找到server字段下的location,更改如下
.......
.......
server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
charset utf-8;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
            add_header Content-Disposition attachment;
            }
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
.......
....... 

新建一些测试文件和文件夹

# cd /usr/share/nginx/html/
# rm -fr *
# touch A{1..9}.txt
# mkdir docs
# mkdir pdf
# chown nginx.nginx /usr/share/nginx/html/ -R
# service nginx reload

浏览器验证访问,如图

标签:Nginx 发布于:2019-11-03 04:01:43