参考文章:http://www.cnxct.com/something-about-phpfpm-s-backlog/
(1)修改系统的backlog cat /proc/sys/net/core/somaxconn 显示为128
编辑文件 vim /etc/sysctl.conf
最后添加 net.core.somaxconn= 1024 保存,然后 sysctl -p
显示为:
[root@bmd_04 ~]# sysctl -p net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1 vm.swappiness = 0 net.ipv4.neigh.default.gc_stale_time = 120 net.ipv4.conf.all.rp_filter = 0 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.all.arp_announce = 2 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 net.core.somaxconn = 1024
可以看到 net.ipv4.tcp_max_syn_backlog = 8192 net.core.somaxconn = 1024
实际会取二者的最小值,所以系统的连接建立后,等待accept的队列大小就是1024
(2)修改nginx的backlog 使用ss -lt命令
[root@bmd_01 ~]# ss -lt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 1 127.0.0.1:8105 *:* LISTEN 0 100 *:8009 *:* LISTEN 0 1 127.0.0.1:8205 *:* LISTEN 0 100 *:8109 *:* LISTEN 0 1 127.0.0.1:9005 *:* LISTEN 0 128 *:8080 *:* LISTEN 0 128 *:http *:* LISTEN 0 100 *:8209 *:*
看到nginx监听的80端口,Send-Q是128的
修改nginx的配置文件,增加backlog设置
server{ listen 80 default_server backlog=1024;
保存,然后nginx -s reload
再次ss -lt
[root@bmd_01 tomcat]# ss -lt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 1 127.0.0.1:8105 *:* LISTEN 0 100 *:8009 *:* LISTEN 0 1 127.0.0.1:8205 *:* LISTEN 0 100 *:8109 *:* LISTEN 0 1 127.0.0.1:9005 *:* LISTEN 0 128 *:8080 *:* LISTEN 0 1024 *:http *:* LISTEN 0 100 *:8209 *:*
(3)修改tomcat的backlog
查看tomcat的server.xml里有acceptCount参数设置,这个实际就是tomcat的backlog值 设置500,表示所有处理线程满了后,再接收500个连接放到队列里,再多的连接就直接拒绝了。acceptCount默认值是100.
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" executor="tomcatThreadPool" connectionTimeout="20000" redirectPort="8443" acceptCount="500" />
上面修改了系统的backlog为1024,但是ss -lt仍然显示是128,只是需要重启一下tomcat,就可以了
[root@bmd_01 conf]# ss -lt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 *:8009 *:* LISTEN 0 1 127.0.0.1:8105 *:* LISTEN 0 1 127.0.0.1:8205 *:* LISTEN 0 100 *:8109 *:* LISTEN 0 1 127.0.0.1:9005 *:* LISTEN 0 500 *:8080 *:* LISTEN 0 1024 *:http *:* LISTEN 0 100 *:8209 *:*
可以看到8080端口的队列大小变成了500. 8105,8205是tomcat的shutdown端口,队列大小是1,正常。
(4)backlog数要根据系统的处理能力来设置,不能写的太离谱~~ 太离谱的话,连接是接进来了,但是处理不过来,调用者那边一般都有超时的,结果还是失败。
|