Nginx搭建上传服务器
使用 Nginx Upload Module 上传文件
安装编译环境:yum -y install gcc gcc-c++kernel-devel
创建目录:mkdir /tmp/nginx
进入目录:cd /tmp/nginx
下载安装包:
wget http://www.openssl.org/source/openssl-1.0.2j.tar.gz
wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz
wget http://nginx.org/download/nginx-1.10.2.tar.gz
git clone https://github.com/vkholodkov/nginx-upload-module
解压:
tar zxf openssl-1.0.2j.tar.gz
tar zxf pcre-8.39.tar.gz
tar zxf zlib-1.2.11.tar.gz
tar zxf nginx-1.10.2.tar.gz
cd nginx-1.10.2
编译:
./configure --add-module=../nginx-upload-module --with-openssl=../openssl-1.0.2j --with-http_gzip_static_module --with-http_ssl_module --with-pcre=../pcre-8.39/ --with-zlib=../zlib-1.2.11
make && sudo make install
配置 nginx.conf
cd /usr/local/nginx/conf
#文件下载
location /download {
alias /nas1/file/maifeng/;
#关闭目录结构(视情况是否打开)
autoindex off;
}
#文件上传
location /upload {
client_max_body_size 50m;
# 转到后台处理URL
upload_pass @download;
# 临时保存路径 (暂时保存此处,使用回调处理,将临时文件变成真实有效文件)
# # 可以使用散列
upload_store /tmp/nginx-upload;
upload_pass_args on;
# 上传文件的权限,rw表示读写 r只读
upload_store_access user:rw;
# 这里写入http报头,pass到后台页面后能获取这里set的报头字段
upload_set_form_field "${upload_field_name}_name" $upload_file_name;
upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
# Upload模块自动生成的一些信息,如文件大小与文件md5值
upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
# 允许的字段,允许全部可以 "^.*$"
#upload_pass_form_field "^submit$|^description$";
upload_pass_form_field "^.*$";
# 每秒字节速度控制,0表示不受控制,默认0
upload_limit_rate 0;
# 如果pass页面是以下状态码,就删除此次上传的临时文件
upload_cleanup 400 404 499 500-505;
}
# proxy_pass 不支持uri添加/(可以使用alias),下面配置等同于访问:http://localhost:7992/download
location @download {
#后端处理
#rewrite ^ /download$1 break;
#proxy_pass http://10.1.1.203:9000;
# 如果不需要后端程序处理,直接返回200即可
return 200;
}
代码参考:https://hui.lu/upload-file-with-nginx-upload-file/
postman调用:https://blog.csdn.net/maowendi/article/details/80537304