nginx的Connection refused

问题描述

nginx的错误日志中突然出现大量的的Connection refused问题,日志如下:

2020/03/19 09:52:53 [error] 20117#20117: *7403411764 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: , request: "POST /post/result/lol?type=Bet HTTP/1.1", upstream: "http://xxx.xxx.xxx.xxx/post/result/lol?type=Bet", host: "xxx.xxx.xxx.xxx"
2020/03/19 09:52:53 [error] 20117#20117: *7403411774 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: , request: "POST /post/result/csgo?type=RollingBet HTTP/1.1", upstream: "http://xxx.xxx.xxx.xxx/post/result/csgo?type=RollingBet", host: "xxx.xxx.xxx.xxx"
2020/03/19 09:52:54 [error] 20116#20116: *7403411815 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: , request: "POST /post/result/lol?type=Bet HTTP/1.1", upstream: "http://xxx.xxx.xxx.xxx/post/result/lol?type=Bet", host: "xxx.xxx.xxx.xxx"

出现这个问题,一开始以为是server节点挂掉,但是查看了下server运行正常;这个错误是突然间爆发大量的错误,查看了相关nginx和服务器监控系统,看到连接数突增。可以说明在高负载下,系统响应变慢,并出现超时或失误失败情况,TIME_WAIT积压。

问题定位

  • 查看了tcp连接命令
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
TIME_WAIT 35423
CLOSE_WAIT 23602
SYN_SENT 62
FIN_WAIT1 61
FIN_WAIT2 259
ESTABLISHED 7543
SYN_RECV 3
CLOSING 35
LAST_ACK 507

发现WAIT数量过高,TCP连接断开后,会以TIME_WAIT状态保留一定的时间,然后才会释放端口。当并发请求过多的时候,就会产生大量的TIME_WAIT状态的连接,无法及时断开的话,会占用大量的端口资源和服务器资源,导致很多连接被拒绝了。

  • 修改系统参数
# vim /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 30 #保留 FIN_WAIT2 的时间, 默认值是60, 单位是秒.
net.ipv4.tcp_timestamps = 1 #时间戳可以避免序列号的卷绕,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 #表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 # 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
  • 配置生效
/sbin/sysctl -p
  • 验证效果
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
TIME_WAIT 2521
CLOSE_WAIT 13602

WAIT的数量降低了,nginx也没有报Connection refused。

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
As an AI language model, I cannot access your server logs or configurations to determine the root cause of the "nginx connection refused" error. However, this error usually occurs when the nginx server is unable to establish a connection with the upstream server or the application server. Here are some possible reasons why you may encounter the "nginx connection refused" error: 1. The upstream server or application server is down or not responding. 2. The firewall or network settings are blocking the connection between the nginx server and the upstream server. 3. The nginx server is not configured to listen on the correct port. 4. The upstream server or application server is not configured to accept connections from the nginx server. 5. The upstream server or application server is overloaded or experiencing high traffic. To resolve the "nginx connection refused" error, you may need to perform the following troubleshooting steps: 1. Check the status of the upstream server or application server and ensure that it is running. 2. Check the network settings and firewall rules to ensure that the nginx server is allowed to connect to the upstream server. 3. Check the nginx configuration file and ensure that it is configured to listen on the correct port. 4. Check the upstream server or application server configuration and ensure that it is configured to accept connections from the nginx server. 5. If the upstream server or application server is overloaded, you may need to optimize its performance or add more resources to handle the traffic. If you are still unable to resolve the error, you may need to seek assistance from a qualified server administrator or web developer.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值