美文网首页
Nginx下载文件中断问题

Nginx下载文件中断问题

作者: Misout | 来源:发表于2019-05-16 11:28 被阅读0次

    问题现象
    1、浏览器发起一个下载请求,例如/export
    2、服务器接收到请求,相应一个attachment filename=具体文件名的相应,并开始将数据写入HTTPServletResponse的outputStream流中
    3、客户端接收到下载文件名响应,用户点击保存后,开始传输数据
    4、服务区outputStream流向客户端flush数据
    5、客户端接收数据
    6、服务器端未发送完数据,浏览器下载文件到一半就提示下载完成。
    7、服务器端报:Caused by: java.net.SocketException: Connection reset异常。

    问题排查
    由于应用属于Web应用,浏览器的请求通过nginx进行转发和负载均衡。发现nginx的请求访问日志,截取了关键信息如下:

    2019/05/15 10:36:57 [warn] 198697#0: *14686663 an upstream response is buffered to a temporary file /dev/shm/nginx_temp/proxy/5/24/0000000245 while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionAdgroup/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionAdgroup/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionAdgroup/?platform=1&device=-1"
    2019/05/15 10:39:17 [warn] 198697#0: *14687121 an upstream response is buffered to a temporary file /dev/shm/nginx_temp/proxy/6/24/0000000246 while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    2019/05/15 10:39:52 [crit] 198697#0: *14687121 pwrite() "/dev/shm/nginx_temp/proxy/6/24/0000000246" failed (28: No space left on device) while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    2019/05/15 11:01:48 [warn] 198694#0: *14690923 an upstream response is buffered to a temporary file /dev/shm/nginx_temp/proxy/7/24/0000000247 while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    2019/05/15 11:02:22 [crit] 198694#0: *14690923 pwrite() "/dev/shm/nginx_temp/proxy/7/24/0000000247" failed (28: No space left on device) while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    2019/05/15 11:16:13 [warn] 198697#0: *14693421 an upstream response is buffered to a temporary file /dev/shm/nginx_temp/proxy/8/24/0000000248 while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    2019/05/15 11:16:48 [crit] 198697#0: *14693421 pwrite() "/dev/shm/nginx_temp/proxy/8/24/0000000248" failed (28: No space left on device) while reading upstream, client: 172.31.127.6, server: sk.jd.com, request: "POST /unionCreative/export HTTP/1.1", upstream: "http://127.0.0.1:1601/unionCreative/export", host: "sk.jd.com", referrer: "http://sk.jd.com/unionCreative/?platform=1&device=-1"
    

    其中特别注意到

    an upstream response is buffered to a temporary file /dev/shm/nginx_temp/proxy/6/24/0000000246 while reading upstream
    pwrite() "/dev/shm/nginx_temp/proxy/6/24/0000000246" failed (28: No space left on device) while reading upstream
    

    再结合nginx官网的说明,传输的数据被先写到了nginx的临时buffer中,再向浏览器客户端传输数据。但buffer的空间不足,导致写失败,下载异常。

    问题解决
    1、针对下载类的请求,文件过大时,针对这类URL设置nginx的location里面的proxy_buffering参数为off。
    默认情况下proxy_buffering这个参数是on状态,关闭后,nginx会对服务器端的数据实时传输给客户端,不会先写buffer后再响应数据。

    相关文章

      网友评论

          本文标题:Nginx下载文件中断问题

          本文链接:https://www.haomeiwen.com/subject/xbbbaqtx.html