Nginx 中 fastcgi_pass 監聽端口 unix socket和tcp socket差

 

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操做系統裏的一種組件。php

TCP和unix domain socket方式對比nginx

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差異不大,但在壓力比較滿的時候,用套接字方式,效果確實比較好。app

下面是php 5.3以上版本將TCP改爲socket方式的配置方法:dom

修改php-fpm.conf(/usr/local/php/etc/php-fpm.conf)socket

;listen = 127.0.0.1:9000
listen = /dev/shm/php-cgi.sock

修改nginx配置文件server段的配置,將http的方式改成socket方式tcp

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與nginxphp-fpm

service nginx restart
service php-fpm restart
ls -al /dev/shm

能夠看到php-cgi.sock文件unix套接字類型post

理論上,unix socket 不走網絡,效率高一些,但穩定性不是很理想,看這個測試:

http://blog.csdn.net/liv2005/article/details/7741732

相關文章
相關標籤/搜索