简介
NginxUploadProgressModule是一个可以可以获取文件上传进度的模块,官方文档:HttpUploadProgressModule
原理
nginx作为代理服务,在代理到后端服务之前,将请求内容全部缓存到磁盘上,通过每次上传的一个唯一标识,能够获取当前服务器已经接收的大小。
安装
1.下载模块,通过git或者http,地址:http://github.com/masterzen/nginx-upload-progress-module/tree/master
2.通过add-modue指令编译到nginx中
指令集
upload_progress
语法:upload_progress <zone_name> <zone_size>;上下文:http
作用:声名nginx server使用upload progress module,引用名为zone_name,并分配zone_size bytes的空间存放上传状态信息
track_uploads
语法:track_uploads <zone_name> <timeout>;上下文:location
作用:声名此location使用upload_progress模块记录文件上传,这条指令必须位于location配置的最后。
report_uploads
语法:report_uploads <zone_name>上下文:location
作用:允许一个location响应上传状态,响应内容默认为一个javascript的new object语句对象,有四种状态
响应:
上传开始(准备中或者请求未到达)new Object({"state":"starting"})正在上传
new Object({"state":"uploading","received":<size_received>,"size":<total_size>})上传完成
new Object({"state":"done"})上传错误
new Object({"state":"error","status":<error code>})
upload_progress_content_type
语法:upload_progress_content_type <content-type>上下文:location
作用:状态响应的content-type,默认为test/javascript,即可执行的javascript代码
upload_progress_header
语法:upload_progress_header <progress-id>
上下文:location作用:修改标识上传ID的参数(header)名,默认为X-Progress-ID
upload_progress_jsonp_parameter
语法:upload_progress_jsonp_parameter <callback_parameter>上下文:location
作用:修改jsonp形式的callback函数名,默认值为“callback”
upload_progress_json_output
语法:upload_progress_json_output上下文:location
作用:声名响应输出为json格式
upload_progress_jsonp_output
语法:upload_progress_jsonp_output上下文:location
作用:声名响应输出为jsonp格式
upload_progress_template
语法:upload_progress_template <state> <template>上下文:location
作用:使用自定义的模板输出响应
state可选:starting、uploading、error、done
模板中可渲染变量:$uploadprogress_length、$uploadprogress_received、$uploadprogress_status、$uploadprogress_callback
示例
http { # reserve 1MB under the name ‘proxied‘ to track uploads upload_progress proxied 1m; server { listen 127.0.0.1 default; server_name localhost; root /path/to/root; location / { # proxy to upstream server proxy_pass http://127.0.0.1; proxy_redirect default; # track uploads in the ‘proxied‘ zone # remember connections for 30s after they finished track_uploads proxied 30s; } location ^~ /progress { # report uploads tracked in the ‘proxied‘ zone report_uploads proxied; } } }