最近Nginx反向代理遇到了“104: Connection reset by peer”错误,google了一下,这里记录一下。
本文根据众多互联网博客内容整理后形成,引用内容的版权归原始作者所有,仅限于学习研究使用。
1 错误原因:检查链接是否已经close。
upstream发送了RST,将连接重置。
errno = 104错误表明你在对一个对端socket已经关闭的的连接调用write或send方法,在这种情况下,调用write或send方法后,对端socket便会向本端socket发送一个RESET信号,在此之后如果继续执行write或send操作,就会得到errno为104,错误描述为connection reset by peer。
如果对方socket已经执行了close的操作,本端socket还继续在这个连接上收发数据,就会触发对端socket发送RST报文。按照TCP的四次握手原理,这时候本端socket应该也要开始执行close的操作流程了,而不是接着收发数据。
2 可能原因
2.1 链接已经断开
这个应该是最根本的原因。
2.2 数据长度不一致
发送端和接收端事先约定好的数据长度不一致导致的,接收端被通知要收的数据长度小于发送端实际要发送的数据长度。
2.3 Nginx缓存小,timeout太小。
nginx的buffer太小,timeout太小。
nginx http模块添加以下参数配置:
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
client_body_buffer_size 20m;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
gzip_buffers 16 8k;
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
keepalive_timeout 240;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
proxy_connect_timeout 600s;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
2.3 没有设置keepalive
ngx_http_upstream_check_module这个模块,在使用tcp检测后端状态时,只进行了TCP的三次握手,没有主动断开这个连接,而是等待服务端来断开。当后端是nginx或者tomcat时(linux上),超时后后端会发fin包关闭这个连接。这个错误日志recv() failed (104: Connection reset by peer)是在后端为IIS的情况下抛出的,抓包发现IIS并不会发fin包来断开链接,而是在超时后发RST包重置连接,所以导致了这个问题。
从这个问题也反应出ngx_http_upstream_check_module这个模块还是需要完善下检测机制的,如果是在检测后端状态后主动关闭这个连接,应该就不会出现connect reset这个问题
通过修改源代码已经解决了该问题
static ngx_check_conf_t ngx_check_types[] = {
{ NGX_HTTP_CHECK_TCP,
ngx_string("tcp"),
ngx_null_string,
0,
ngx_http_upstream_check_peek_handler,
ngx_http_upstream_check_peek_handler,
NULL,
NULL,
NULL,
0,
1 },
将最后一行的1改为0即可,根据数据结构分析可得知,这个1代表启用keepalived,所以客户端才不会主动断开连接,因为这是tcp的端口连通性检查,不需要keepalived,将其改为0禁止keepalived即可。
修改之后的代码如下:
static ngx_check_conf_t ngx_check_types[] = {
{ NGX_HTTP_CHECK_TCP,
ngx_string("tcp"),
ngx_null_string,
0,
ngx_http_upstream_check_peek_handler,
ngx_http_upstream_check_peek_handler,
NULL,
NULL,
NULL,
0,
0 },
2.4 设置lingering_close
即使你禁用了 http keepalive,nginx 仍然会尝试处理 HTTP 1.1 pipeline 的请求。你可以配置
lingering_close off 禁用此行为,但这不是推荐的做法,因为会违反 HTTP 协议。见
http://nginx.org/en/docs/http/ngx_http_core_module.html#lingering_close
3 nginx快速定位异常
错误信息 | 错误说明 |
"upstream prematurely(过早的) closed connection" | 请求uri的时候出现的异常,是由于upstream还未返回应答给用户时用户断掉连接造成的,对系统没有影响,可以忽略 |
"recv() failed (104: Connection reset by peer)" | (1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉; (2)客户关掉了浏览器,而服务器还在给客户端发送数据; (3)浏览器端按了Stop |
"(111: Connection refused) while connecting to upstream" | 用户在连接时,若遇到后端upstream挂掉或者不通,会收到该错误 |
"(111: Connection refused) while reading response header from upstream" | 用户在连接成功后读取数据时,若遇到后端upstream挂掉或者不通,会收到该错误 |
"(111: Connection refused) while sending request to upstream" | Nginx和upstream连接成功后发送数据时,若遇到后端upstream挂掉或者不通,会收到该错误 |
"(110: Connection timed out) while connecting to upstream" | nginx连接后面的upstream时超时 |
"(110: Connection timed out) while reading upstream" | nginx读取来自upstream的响应时超时
|
"(110: Connection timed out) while reading response header from upstream" | nginx读取来自upstream的响应头时超时 |
"(110: Connection timed out) while reading upstream" | nginx读取来自upstream的响应时超时 |
"(104: Connection reset by peer) while connecting to upstream" | upstream发送了RST,将连接重置 |
"upstream sent invalid header while reading response header from upstream" | upstream发送的响应头无效 |
"upstream sent no valid HTTP/1.0 header while reading response header from upstream" | upstream发送的响应头无效 |
"client intended to send too large body" | 用于设置允许接受的客户端请求内容的最大值,默认值是1M,client发送的body超过了设置值 |
"reopening logs" | 用户发送kill -USR1命令 |
"gracefully shutting down", | 用户发送kill -WINCH命令 |
"no servers are inside upstream" | upstream下未配置server |
"no live upstreams while connecting to upstream" | upstream下的server全都挂了 |
"SSL_do_handshake() failed" | SSL握手失败 |
"ngx_slab_alloc() failed: no memory in SSL session shared cache" | ssl_session_cache大小不够等原因造成 |
"could not add new SSL session to the session cache while SSL handshaking" | ssl_session_cache大小不够等原因造成 |
参考:
https://github.com/alibaba/tengine/issues/901
https://my.oschina.net/u/1024107/blog/1838968
https://blog.csdn.net/zjk2752/article/details/21236725
http://nginx.org/en/docs/http/ngx_http_core_module.html#lingering_close
https://blog.csdn.net/crj121624/article/details/79956283
请我喝咖啡
如果觉得文章写得不错,能对你有帮助,可以扫描我的微信二维码请我喝咖啡哦~~哈哈~~