nginx 反向代理上传文件时 104

nginx 104 error

场景复现

上传文件时,nginx 日志中总是能看到类似:failed (104: Connection reset by peer) while reading upstream 的报错输出

故障分析

  • 一开始以为是后端程序出了问题,但是经过测试后发现程序正常,上传图片功能可以使用
  • 网上搜索之后发现,nginx 默认使用的 http 版本是 1.0 的,而客户端上传图片时,采用的协议版本是 http1.1 的。而下载文件采用的是分块传递,nginx 在从后端程序中下载文件时会出现向上不兼容的情况,也由此会导致 104 错误的产生。

解决方案

该 bug 主要是由于 http 版本之间的差异导致的,因此,在反向代理时加入支持 http 1.1 版本即可。

1
2
3
4
5
6
# nginx.conf
location / {
    ...
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

Reference

Nginx: Connection reset by peer 错误定位 errno 104:connetction reset by peer的错误分析

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy