技术解析
服务使用的是 nginx + tomcat
最近发现偶现 502
错误日志:
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 http upstream process header
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 malloc: 00007FB65E3834E0:4096
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 posix_memalign: 00007FB65C783D60:4096 @16
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 美国服务器recv: eof:1, avail:1
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 recv: fd:65 0 of 4096
2018/10/17 01:31:05 [error] 46093#46093: *606728052 upstream prematurely closed connection while reading response header from upstream, clien
t:xxx
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 http next upstream, 2
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 free keepalive peer
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 free rr peer 4 4
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 free rr peer failed: 00007FB65C704F50 1
2018/10/17 01:31:05 [debug] 46093#46093: *606728052 finalize http upstream request: 502
看了下 nginx 代码,发现是
c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);
读到的内容为 0,日志中也有显示recv: fd:65 0 of 4096
,一开始就读不到(查了 tomcat 日志,请求还没到 tomcat )
https://github.com/nginx/nginx/blob/1305b8414d22610b0820f6df5841418bf98fc370/src/http/ngx_http_upstream.c#L2366
怀疑是各种原因导致建立的这个长链接断掉了。试着把 keeplaive 去掉就好了。但是这个会影响性能,不知道大家怎么解决这个问题?
还有人可能会问为什么没有重试,那是因为 proxy_next_upstream 只设置了 error、timeout,没有设置 non_idempotent (这是个 POST 请求)