解决nginx+php+yii2下time_wait连接数过多问题

查看连接数

netstat -n |  wc -l
netstat -n | grep -i time_wait | wc -l

发现9000和3306的time_out状态的连接数过多, 网上大部分解决方案是修改sysctl.conf回收重用ipv4连接, 但是这在公司的阿里云机器上好像行不通,回导致无法访问。

tcp复用解决方案

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30

解决nginx time_wait连接数过多问题

修改nginx.conf

upstream fastcgi_backend {
    server 127.0.0.1:9000;
    keepalive 60;
}

location ~ \.php$ {
        fastcgi_pass  fastcgi_backend;
        fastcgi_keep_conn on;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

解决mysql time_out连接数过多

修改yii数据库配置采用长链接

'db' => [ 
    'class' => 'yii\db\Connection',
    'dsn' => '*', 'username' => '*', 
    'password' => '*', 
    'charset' => 'utf8mb4', 
    'attributes' => [ 
       PDO::ATTR_PERSISTENT => true 
    ]
],

发表评论