Nginx支持http文件上传的配置

一.相关模块安装
  查看Nginx是否安装了这两个模块(nginx_upload_module和nginx_uploadprogress_module),
  使用命令:
  $ nginx -V (注意是大写),可以查看Nginx当时编译时候的参数,如果发现有上述两个模块,说明Nginx已经安装了这两个模块。
  如果没有的话,就需要安装这两个Nginx模块。
1.下载nginx_upload_modul
  下载链接:http://www.grid.net.ru/nginx/upload.en.html
  $ wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
  $ tar xvzf nginx_upload_module-2.2.0.tar.gz

2.下载nginx_uploadprogress_module
  网址:http://wiki.nginx.org/HttpUploadProgressModule
  下载链接:https://github.com/masterzen/nginx-upload-progress-module/tree/master
  $ unzip nginx-upload-progress-module-master.zip

 

二.Nginx添加编译选项
  由于这两个模块不在Nginx源代码中,需要重新编译Nginx,在编译选项中加上
  --add-module=/模块源代码路径/nginx_upload_module-2.2.0
  --add-module=/模块源代码路径/nginx-upload-progress-module-maste。

如下示例所示:

./configure --prefix=/data/nginx --with-http_ssl_module --with-http_sub_module --add-module=/data/nginx/nginx-upload-module-master --add-module=/data/nginx/masterzen-nginx-upload-progress-module-82b35fc

$ make
$ make install

  make编译时的错误提示:

/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c: In function 'ngx_http_read_upload_client_request_body':
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2628: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2687: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c: In function 'ngx_http_do_read_upload_client_request_body':
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2769: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2785: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2877: error: 'ngx_http_request_body_t' has no member named 'to_write'
make[1]: *** [objs/addon/nginx_upload_module-2.2.0/ngx_http_upload_module.o] Error 1
make[1]: Leaving directory `/opt/nginx_http_rtmp/nginx-1.6.0'
make: *** [build] Error 2

  解决办法:https://github.com/fdintino/nginx-upload-module下载nginx-upload-module-master.zip


三.配置Nginx,实现上传模块来接收页面上传的文件
  把下面配置添加到Nginx的配置文件中,注意是加在server的上下文中。

location = /upload {
    upload_pass /service.php?path=uploadfile&a=upload_server;//表示Nginx接收完上传的文件后,然后交给后端处理的地址
    upload_cleanup 400 404 499 500-505; //表示当发生这些http status代码的情况下,会把上传的文件删除
    upload_store /tmp/upload_tmp 1;//上传模块接收到的文件临时存放的路径, 1 表示方式,该方式是需要在/tmp/upload_tmp下创建以0到9为目录名称的目录,上传时候会进行一个散列处理。
    upload_store_access user:r; //指定访问模式
    upload_limit_rate 128k; //设定上传速度上限
    upload_set_form_field "${upload_field_name}_name" $upload_file_name; //设定后续脚本语言访问的变量,其中${upload_field_name}对照本例子就是addfile。比如后台PHP就可以通过$_POST['addfile_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_store设置了临时文件存放根路径,该路径就是经过散裂后上传文件存在真实路径,比如后续处理可以根据这值把上传文件拷贝或者移动到指定的目录下。
    upload_pass_form_field "^.*$";//
    upload_pass_args on;// 打开开关,意思就是把前端脚本请求的参数会传给后端的脚本语言,比如:http://192.168.1.203:7100/upload/?k=23.PHP脚本可以通过$_POST['k']来访问。
}

  也可进一步参看:http://www.grid.net.ru/nginx/upload.en.html中的说明和配置实例。

  上述配置完了,就可以实现上传的功能了。

 

四.获取上传的进度
  要获取上传的进度,那还是需要配置另外一个模块nginx_uploadprogress_module。
  其实,获取当前进度原理比较简单,就是通过javascript以异步方式定时给特定地址发送请求,
  这个模块会以json格式返回上传的进度。配置比较简单。
1.首先打开这个模块功能,在Nginx配置文件中http上下文里面,增加upload_progress proxied 5m;
  其中,proxied表示名称(zone_name官方文档),5m表示每次链接存放跟踪信息的大小。
  另外,再设置返回格式为json,upload_progress_json_output;
2.在上述的location = /upload中增加一个配置项track_uploads proxied 30s;
  其中,proxied就是刚才在第一步设置的名字,30s表示每次链接处理完毕后,链接会保持30s。
3.设置一个location来处理javascript发送请求。

location ^~ /progress {
  report_uploads proxied; #GET此地址得到上传进度
}

4.还有一个参数考虑设置upload_progress_header ,这个值缺省是X-Progress-ID。

  有点类似SessionID,主要用在前台需要在上传文件的时候需要设置这个参数值,比如设置为uuid值。
  这样javascript每次发送请求要获取上传进度时候,都需要带上这个参数,这样上传进度跟踪模块才知道是返回那个链接的进度。
经过这几个步骤,就把上传进度跟踪模块配置好了。

 

文章来源:https://blog.csdn.net/fireroll/article/details/43937637

参考文章:https://www.nginx.com/resources/wiki/modules/upload_progress/

http://www.grid.net.ru/nginx/upload.en.html

posted on 2018-08-15 22:28  bijian1013  阅读(5501)  评论(0编辑  收藏  举报

导航