nginx与php-fpm通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cjqh_hao/article/details/80822599

nginx和php-fpm通信有tcp socket和unix socket两种方式。

unix socket方式
优点:
unix socket要比tcp快,且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还要申请临时端口和tcp相关资源。

缺点:
unix socket相比不是那么稳定,当并发连接数爆发时,会产生大量的长时缓存,在没有面向连接协议支撑的情况下,大数据包很有可能就直接出错并不会返回异常。

虽然socket有更少的数据拷贝和上下文切换,更少的资源占用,但是如果数据都是错的,那还有什么用呢。另外使用unix socket的话,必须nginx和fpm在同一台机器上

tcp socket方式
优点:
从稳妥的考虑肯定是使用tcp,tcp协议能保证数据的正确性,unix socket不能保证。可以跨服务器,当nginx和php-fpm不在同一台机器上时,只能使用这种方式。

缺点:
性能不如unix socket

配置信息

#tcp socket方式

location  / { 
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    include        fastcgi_params;
}   
#unix socket方式

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
}

参考文档
https://www.cnblogs.com/luoahong/articles/9139888.html

猜你喜欢

转载自blog.csdn.net/cjqh_hao/article/details/80822599