nginx带进度条的上传超大文件

北慕城南 提交于 2019-11-28 22:20:56

11年写的 
http://haoningabc.iteye.com/blog/1711534 
重新整理下 
准备---------------------------------------------- 

nginx-1.8.1.tar.gz 能过, 
  1.10不行,会有openssl md5之类的错误 

上传 
https://github.com/vkholodkov/nginx-upload-module/tree/2.2 
nginx-upload-module-2.2.zip  

上传进度条 
https://www.nginx.com/resources/wiki/modules/upload_progress/ 
使用v0.8.4 的版本 
masterzen-nginx-upload-progress-module-v0.8.4-0-g82b35fc.tar.gz 

上传进度条客户端: 
https://github.com/drogus/jquery-upload-progress 



跑php的fcgi 
spawn-fcgi-1.6.3.tar.bz2 


yum install openssl-devel zlib-devel prce-devel -y 
这些 nginx需要用到的 

yum install php php-devel -y 

安装----------------------------------------- 

安装spawn-fcgi 

Java代码 

 

  1. tar xvf spawn-fcgi-1.6.3.tar.bz2  
  2. ./configure --prefix=/usr/local/spawn  
  3. make  
  4. make install  



启动脚本 
runcgi.sh 
 

Java代码 

 

  1. #  
  2. !/bin/sh  
  3. export PHP_FCGI_MAX_REQUESTS=0  
  4. /usr/local/spawn/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u haoning -g haoning -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid  




安装nginx 
编译nginx 

Java代码 

 

  1. ./configure --prefix=/usr/local/nginx  --add-module=/opt/masterzen-nginx-upload-progress-module-82b35fc --add-module=/opt/nginx-upload-module-2.2   
  2. make  
  3. make install  



nginx配置文件 

Java代码 

 

  1. worker_processes  1;    
  2. events {    
  3.     worker_connections  1024;    
  4. }    
  5. http {    
  6.     autoindex on;     
  7.     autoindex_exact_size off;     
  8.     autoindex_localtime on;     
  9.     default_type application/octet-stream;     
  10.     sendfile on;     
  11.     tcp_nopush on;    
  12.     tcp_nodelay on;    
  13.     keepalive_timeout 10;    
  14.     gzip on;    
  15.     gzip_min_length 1k;    
  16.     gzip_buffers 4 8k;    
  17.     gzip_http_version 1.1;    
  18.     gzip_comp_level 3;    
  19.     gzip_types text/css text/xml text/plain application/x-javascript application/xml application/pdf application/rtf application/x-perl application/x-tcl application/msword application/vnd.ms-excel application/vnd.ms-powerpoint application/vnd.wap.xhtml+xml image/x-ms-bmp;    
  20.     gzip_vary on;    
  21.     output_buffers 4 32k;    
  22.     upload_progress_json_output;    
  23.     upload_progress proxied 1m;    
  24.     server {    
  25.         listen       80;    
  26.         server_name  192.168.139.114;    
  27.         charset utf-8,gb2312;    
  28.         client_max_body_size 12000m;    
  29.         location /upload {    
  30.                 upload_pass   @test;    
  31.                 #upload_store /tmp 1;   
  32.         upload_store /tmp;     
  33.                 upload_store_access user:r;    
  34.                 upload_set_form_field "${upload_field_name}_name" $upload_file_name;    
  35.                 upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;    
  36.                 upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;    
  37.                 upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;    
  38.                 upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;    
  39.                 upload_pass_form_field "^submit$|^description$";    
  40.                 track_uploads proxied 30s;    
  41.         }       
  42.         location @test {    
  43.                 rewrite ^(.*)$  /test.php last;    
  44.         }       
  45.     
  46.         location / {    
  47.             proxy_set_header Host $http_host;    
  48.             root   html;    
  49.             index  index.html index.htm index.php;    
  50.         }    
  51.     
  52.         location ~ (.*)/x-progress-id:(\w*) {    
  53.             rewrite ^(.*)/x-progress-id:(\w*)   $1?X-Progress-ID=$2;    
  54.         }    
  55.         location ^~ /progress {    
  56.             report_uploads proxied;    
  57.         }    
  58.         location ~ \.php$ {     
  59.             fastcgi_pass 127.0.0.1:9000;    
  60.             fastcgi_index index.php;    
  61.             set $path_info "/";    
  62.             set $real_script_name $fastcgi_script_name;    
  63.             if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { set $real_script_name $1;    
  64.                 set $path_info $2;    
  65.             }     
  66.         }     
  67.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {     
  68.             root html;    
  69.             access_log off;    
  70.             expires 30d;    
  71.         }     
  72.         location ~ .*\.(js|css|ico)?$ {     
  73.             root html;    
  74.             access_log off;    
  75.             expires 1h;    
  76.         }     
  77.         error_page 500 502 503 504 /50x.html;    
  78.         location = /50x.html {     
  79.             root html;    
  80.         }     
  81.         fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;    
  82.         fastcgi_param script_name $real_script_name;    
  83.         fastcgi_param path_info $path_info;    
  84.         include /usr/local/nginx/conf/fastcgi_params;     
  85.     }    
  86.     
  87. }  



客户端 
jquery-upload-progress-master.zip 
解压到 
/usr/local/nginx/html/ 
cd  /usr/local/nginx/html/jquery-upload-progress-master/example 
vim index.html 

Java代码 

 

  1. <body>  
  2.           <form id="upload" enctype="multipart/form-data" action="index.html" method="post">  
  3.         <input name="file" type="file"/>  
  4.         <input type="submit" value="Upload"/>  
  5.       </form>  


改成 

Java代码 

 

  1. <body>  
  2.           <form id="upload" enctype="multipart/form-data" action="/upload" method="post">  
  3.         <input name="file" type="file"/>  
  4.         <input type="submit" value="Upload"/>  
  5.       </form>  


因为nginx配置的/upload 

加一个请求返回的php返回值 
test.php 

Java代码 

 

  1. <?php    
  2. print_r($_POST);    
  3. ?>   





启动 

Java代码 

 

  1. export PHP_FCGI_MAX_REQUESTS=0  
  2. /usr/local/spawn/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u haoning -g haoning -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid  
  3.   
  4. /usr/local/nginx/sbin/nginx  




cd /tmp 
watch -n 1  'ls -lh' 
查看上传的文件 

chmod -R 777 /tmp 

浏览器访问 
http://192.168.139.122/jquery-upload-progress-master/example/ 



效果

 









 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!