nginx目录索引、文件下载服务

目录索引、文件下载服务

官网文档
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

 

利用nginx实现文件下载服务器

1.参数说明

Syntax:    autoindex on | off;
Default:    
autoindex off;
Context:    http, server, location


# autoindex on   ; 表示开启目录索引


Syntax:    autoindex_localtime on | off;
Default:    
autoindex_localtime off;
Context:    http, server, location


# autoindex_localtime on; 显示文件为服务器的时间


Syntax:    autoindex_exact_size on | off;
Default:    
autoindex_exact_size on;
Context:    http, server, location

# autoindex_exact_size on; 显示确切bytes单位
# autoindex_exact_size off; 显示文件大概单位,是KB、MB、GB


若目录有中文,nginx.conf中添加utf8编码
charset utf-8,gbk;

 

2.配置文件

测试数据

[root@web-9 /var/log/nginx]#dd if=/dev/zero of=/yuchaoit/牛啊牛啊.log^C
[root@web-9 /var/log/nginx]#ll /yuchaoit/ -h
total 236M
-rw-r--r-- 1 root root 236M May  8 16:06 牛啊牛啊.log
-rw-r--r-- 1 root root    0 May  8 16:04 男宾10号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾1号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾2号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾3号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾4号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾5号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾6号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾7号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾8号.png
-rw-r--r-- 1 root root    0 May  8 16:04 男宾9号.png
单独生成子配置文件,专用于下载


cat >/etc/nginx/conf.d/download.conf <<EOF
server{

    listen 8888;
    server_name localhost;
    charset utf-8,gbk;
    location / {

        root /yuchaoit/;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
    }
}
EOF

 

重启,查看结果

systemctl restart nginx

 

3.访问目录索引

 

posted @ 2023-06-13 10:21  Mrterrific  阅读(387)  评论(0编辑  收藏  举报