HAProxy后端Undertow服务日志出现Connection reset问题排查
HAProxy与Undertow连接超时问题排查
环境:Ubuntu服务器部署Undertow 2.3.2服务,前端使用HAProxy 2.6.15,未修改HAProxy任何超时或其他配置,采用默认设置。
两种调用场景的日志表现
- 直接调用Undertow接口:访问
http://localhost:12345/api/getSomeData,请求完成约1分钟后,仅出现闲置连接超时的调试日志:
2023-12-04 09:10:54,688 INFO [XNIO-1 task-2] testutils.RestApi - com.test.utils.testutils.RestApi (RestApi.java:64) - Getting user 2023-12-04 09:12:01,524 DEBUG [XNIO-1 I/O-3] protocol.ParseTimeoutUpdater - io.undertow.request (ParseTimeoutUpdater.java:151) - Timing out idle connection from /[0:0:0:0:0:0:0:1]:55438
- 通过HAProxy转发调用:访问
http://localhost/api/getSomeData,请求完成15秒后,Undertow日志出现连接重置的异常堆栈:
2023-12-04 09:01:45,046 INFO [XNIO-1 task-2] testutils.RestApi - com.test.utils.testutils.RestApi (RestApi.java:64) - Getting user 2023-12-04 09:02:00,770 DEBUG [XNIO-1 I/O-6] http.HttpReadListener - io.undertow.request.io (HttpReadListener.java:160) - Error reading request java.net.SocketException: Connection reset at sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:401) ~[?:?] at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:434) ~[?:?] at org.xnio.nio.NioSocketConduit.read(NioSocketConduit.java:289) ~[xnio-nio-3.8.8.Final.jar:3.8.8.Final] at org.xnio.conduits.ConduitStreamSourceChannel.read(ConduitStreamSourceChannel.java:127) ~[xnio-api-3.8.8.Final.jar:3.8.8.Final] at io.undertow.server.protocol.http.HttpReadListener.handleEventWithNoRunningRequest(HttpReadListener.java:158) [undertow-core-2.3.2.Final.jar:2.3.2.Final] at io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:136) [undertow-core-2.3.2.Final.jar:2.3.2.Final] at io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:59) [undertow-core-2.3.2.Final.jar:2.3.2.Final] at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92) [xnio-api-3.8.8.Final.jar:3.8.8.Final] at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66) [xnio-api-3.8.8.Final.jar:3.8.8.Final] at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:89) [xnio-nio-3.8.8.Final.jar:3.8.8.Final] at org.xnio.nio.WorkerThread.run(WorkerThread.java:591) [xnio-nio-3.8.8.Final.jar:3.8.8.Final]
问题核心原因
出现该差异的本质是HAProxy默认的连接超时设置远短于Undertow的闲置连接超时:
- Undertow默认闲置连接超时为60秒,对应直接调用场景中1分钟后触发的超时提示;
- HAProxy 2.6.x默认
timeout http-keep-alive为15秒,请求完成后若客户端与Undertow均未主动关闭连接,HAProxy会在15秒后主动断开与Undertow的连接,导致Undertow侧读取数据时触发Connection reset异常。
解决方案
修改HAProxy配置文件(通常为/etc/haproxy/haproxy.cfg),调整超时参数使其与Undertow的闲置超时匹配或更长:
- 在
defaults段中添加或修改以下配置:
defaults # 调整HTTP长连接保持超时,与Undertow的60秒匹配 timeout http-keep-alive 60s # 按需调整客户端、服务端超时(可选) timeout client 60s timeout server 60s
- 重启HAProxy服务使配置生效:
systemctl restart haproxy
调整后,HAProxy不会过早断开连接,Undertow可按照自身闲置超时逻辑处理连接,避免出现连接重置异常。
内容的提问来源于stack exchange,提问作者Jonhtra
相关产品推荐
相关产品推荐

