问题表现
公网服务器—>数据中心Nginx代理端口---->内网数据库
实现公网服务器通过ng的代理访问到mysql57.
在访问的过程中稍微大的集合查询就会出现数据库连接错误error2006或erro2013 lost connection
解决思路
1.抓包:发现nginx代理上,会抓到RST包。
2.源公网服务器抓包正常。
3.目标mysql抓包也大量的RST包
4.分析是nginx主动发送rst,断开连接
stream配置如下:4层代理的具体参数参考:https://nginx.org/en/docs/stream/ngx_stream_core_module.html#listen
nginx配置如图
// An highlighted block
log_format proxy '$proxy_add_x_forwarded_for [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
tcp_nodelay off;
server {
access_log logs/proxy_db2.log proxy; #从2代理端口
listen 50004 rcvbuf=10m sndbuf=10m so_keepalive=30s:4s:10; #重点修改的这一样参数,配置Keeplive,表示 30 秒无数据传输时发送探测包,总共发送 10 次,发送时间间隔为系统内核参数 tcp_keepalive_intvl 的设定值
proxy_connect_timeout 3000s;
proxy_timeout 6000s;
proxy_protocol_timeout 600s;
preread_timeout 3000s;
preread_buffer_size 128k;
proxy_pass 10.1.1.5:3306;
}
listen的具体参数配置,需要参考操作系统的tcp长连接机制:
tcp长连接机制需要tcp操作系统的keeplive配置
文章讨论了在使用nginx进行4层(stream)代理时遇到的短连接问题,表现为大查询导致数据库连接错误。解决方案是通过调整nginx配置中的tcp_nodelay和keepalive参数,设置合适的keepalive时间及重试次数,以维持长连接并避免RST包的发送。配置示例中包含了具体的参数设置。

442

被折叠的 条评论
为什么被折叠?



