Nginx 中 fastcgi_pass 監聽端口 unix socket和tcp socket差別
Nginx連接fastcgi的方式有2種:unix domain socket和TCP,Unix domain socket 或者 IPC socket是一種終端,可以使同一台操作系統上的兩個或多個進程進行數據通信。與管道相比,Unix domain sockets 既可以使用字節流和數據隊列,而管道通信則只能通過字節流。Unix domain sockets的接口和Internet socket很像,但它不使用網絡底層協議來通信。Unix domain socket 的功能是POSIX操作系統里的一種組件。
TCP和unix domain socket方式對比
TCP是使用TCP端口連接127.0.0.1:9000
Socket是使用unix domain socket連接套接字/dev/shm/php-cgi.sock(很多教程使用路徑/tmp,而路徑/dev/shm是個tmpfs,速度比磁盤快得多)
fastcgi_pass unix:/tmp/php-cgi.sock fastcgi_pass 127.0.0.1:9000
在服務器壓力不大的情況下,tcp和socket差別不大,但在壓力比較滿的時候,用套接字方式,效果確實比較好。
下面是php 5.3以上版本將TCP改成socket方式的配置方法:
修改php-fpm.conf(/usr/local/php/etc/php-fpm.conf)
;listen = 127.0.0.1:9000 listen = /dev/shm/php-cgi.sock
修改nginx配置文件server段的配置,將http的方式改為socket方式
location ~ .*\.(php|php5)?$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }
重啟php-fpm與nginx
service nginx restart service php-fpm restart ls -al /dev/shm
可以看到php-cgi.sock文件unix套接字類型
理論上,unix socket 不走網絡,效率高一些,但穩定性不是很理想,看這個測試:
http://blog.csdn.net/liv2005/article/details/7741732