原理上来说,unix socket方式肯定要比tcp的方式快而且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还要申请临时端口和tcp相关资源,unix socket会显得不是那么稳定,当并发连接数爆发时,会产生大量的长时缓存,在没有面向连接协议支撑的情况下,大数据包很有可能就直接出错并不会返回异常。而TCP这样的面向连接的协议,多少可以保证通信的正确性和完整性。
touch /var/run/php-fcgi.sock
chmod 777 /var/run/php-fcgi.sock
chown nginx. /var/run/php-fcgi.sock
修改www.conf
listen = /var/run/php-fcgi.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
user = nginx
group = nginx
修改nginx配置文件;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fcgi.sock;
修改sysctl.conf;
# vi /etc/sysctl.conf
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
# sysctl –p
//上面参数是必须的,1是增加端口数量,2是减少time_wait连接过多
先restart php-fpm
然后:nginx -s stop
Nginx
不能用restart,会出错,解决步骤:
ps -aux | grep nginx | awk ‘{print $2}’| xargs kill -9
进行测试
www.conf示例内容:
[www]
;listen = 127.0.0.1:9000
listen = /var/run/php-fcgi.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
pm = dynamic
pm.max_children = 1000
pm.start_servers = 150
pm.min_spare_servers = 50
pm.max_spare_servers = 1000
pm.max_requests = 1000
slowlog = /var/log/php-fpm/www-slow.log
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache