文件上传与下载是web服务器的重要功能。配置nginx文件服务器,支持文件上传和下载功能
官网参考资料:
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
location / { autoindex on; }
http://nginx.org/en/docs/http/ngx_http_dav_module.html
编译:
需要重新编译openresty/nginx, 打开dav模块,编译参数--with-http_dav_module
配置:
location /file {
root /data/;
autoindex_localtime on;
autoindex_exact_size off;
autoindex_format html;
autoindex on;
}
location / {
root /data/file;
client_body_temp_path /data/client_temp;
dav_methods PUT DELETE MKCOL COPY MOVE;
create_full_put_path on;
dav_access group:rw all:r;
#limit_except GET {
# allow 192.168.1.0/32;
# deny all;
#}
}
autoindex_format html
| xml
| json
| jsonp
;
测试:
1. 访问文件列表
http://my_ip:port/file/
[ { "name":"hello_world.txt", "type":"file", "mtime":"Sun, 27 May 2018 00:54:40 GMT", "size":13 }, { "name":"test2.txt", "type":"file", "mtime":"Sun, 27 May 2018 03:54:04 GMT", "size":12 } ]
[ { "name":"hello_world.txt", "type":"file", "mtime":"Sun, 27 May 2018 00:54:40 GMT", "size":13 } ]
2. 下载文件
curl -O http://localhost/file/test2.txt
3. 上传文件
curl -T test3.txt http://localhost/
上传后列表:
../ hello_world.txt 27-May-2018 08:54 13 test2.txt 27-May-2018 11:54 12 test3.txt 27-May-2018 13:15 0