很久之前用python+uwsgi+nginx部署的网站,访问时候502,查看uwsgi日志发现一直提示以下内容
Sat Sep 8 10:56:36 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:37 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:38 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:39 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:40 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:41 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:42 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:43 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:44 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:45 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:46 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:47 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:48 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:49 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:50 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:51 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:52 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:53 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:54 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
Sat Sep 8 10:56:55 2018 - *** uWSGI listen queue of socket "127.0.0.1:8001" (fd: 3) full !!! (101/100) ***
分析
以这个错误为基础,查询了下相关资料,应该是系统级别参数的问题,具体可以参考 linux man page listen(2).
lzz注: 简单的理解就是每个监听的socket,在没有accept之前,等待处理的socket队列长度,linux(至少在centos6.6中)默认是128,在我这个编译的uwsgi中默认是100,也就是说没有调整系统参数之前,最高也就是128。
那么怎样才能把队列的长度调整变长呢?
* 必须调整系统参数,使其生效
* 必须调整uwsgi配置,然后重启应用
操作
修改系统参数
这里直接修改配置文件了,重启后仍然有效。
修改/etc/sysctl.conf文件,添加或者修改这几个参数值
#对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了
net.core.somaxconn = 262144
#表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
net.ipv4.tcp_max_syn_backlog = 8192
#网卡设备将请求放入队列的长度
net.core.netdev_max_backlog = 65536
修改完成之后要记得 sysctl -p
重新加载参数
uwsgi调整
不管是配置,还是命令行加一个选项,例如 .ini 文件中添加如下配置
listen=1024
- 1
之后重启应用,重新加载配置。
小结
通过修改配置,这种错误基本没有出现过了,系统的吞吐量和并发数都大大提高了。所以系统特性和调优对于提高整个服务质量非常重要。