z7369 发表于 2016-12-27 06:33:19

nginx TOMCAT 文件下载 上传 进度条 缓存

 
上传部分
如果在应用服务器里面写了进度条的程序,通过NGINX转发你就坑了,NGINX会把整个附件数据先本地缓存,缓存完毕后转发给你的应用服务器,在这个过程之间,恭喜,你的进度条永远就是0了,待转发后,你的进度条可能瞬间就可以到达一百。好吧,恭喜了,转发了一次。
解决这个问题的方法是配置proxy_request_buffering off ;具体功能见官网说明。
最后讲一句,放在HTTP里面。
 
 
下载部分
 
 
NGinx配置
server {  
charset GBK;
listen       8083;        #端口  
server_name  localhost;   #服务名  
 
   location / {
              proxy_pass   http://127.0.0.1:8001/;#转到的TOMCAT
           }
  location ~ ^/testfile/(.*)\.txt$ {
 
#set $obj_file "$1.txt";
#if (!-f /t1/$obj_file){
# rewrite ^ /t1/ last;
#}
internal; #禁止访问
error_page 404 =200 @backend; #跳转到配置文件
  }
  location @backend {#配置
     rewrite  ^/testfile/(.*)$ /Samples/testfile.jsp?path=$1 break;  #$后面为TOMCAT验证路径
 proxy_pass   http://127.0.0.1:8001;
proxy_redirect          off;  
proxy_set_header        Host $host;  
proxy_set_header        X-Real-IP $remote_addr;  
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
break;  
 
  }
 
}  
 
 
 
 
 
 
JSP部分,
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 //自己获取传过来的参数,进行解析验证是否合法。,合法后把/t1/1.txt的地址换位文件实际地址,切记带/
  response.setHeader("Content-Disposition","attachment;"); 
  response.setHeader("Content-Type","application/octet-stream"); 
  response.setHeader("X-Accel-Redirect","/t1/1.txt"); 
 
 
%>
 
 
 参考资料
  http://toptree.iyunv.com/blog/1275200
  http://www.oschina.net/question/54100_33185
  以下为正则
  http://blog.csdn.net/a519640026/article/details/9138487
  nginx配置
 http://segmentfault.com/a/1190000000437323
 
nginx优化
http://www.z-dig.com/nginx-optimization-25.html#_Toc422324204
页: [1]
查看完整版本: nginx TOMCAT 文件下载 上传 进度条 缓存