nginx php fpm sock_nginx-php TCP通信与Unix domain socket通信

前言

nginx转发php类型的请求可以通过fastcgi的方式,fastcgi支持TCP和 unix domain socket两种方式。

Unix domain socket监听

0ca72ea0c569c2fbbca1e11266e52400.png

Unix domain socket 或者 IPC socket是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。

与管道相比,Unix domain sockets 既可以使用字节流和数据队列,而管道通信则只能通过字节流。

Unix domain sockets的接口和Internet socket很像,但它不使用网络底层协议来通信。Unix domain socket 的功能是POSIX操作系统里的一种组件。

e6ef79e341cc4e0a6cb64da5cc6a13d4.png

TCP监听

通过网络TCP链接建立网络通信,即使是监听127.0.0.1,也是通过网络底层协议来通信。相对于socket方式,会消耗一些网络资源。

选择TCP还是Unix domain socket?

如果nginx和php-fpm都在同一台机器,当然是选择Unix domain socket方式;但是如果nginx和php-fpm不在同一台机器,socket方式就不能用了。

266aa64cd45d1b196b943e83c00a2a44.png

如果网站的静态资源和php处理的页面是分开的,需要较高的处理能力,可以选择将nginx和php-fpm分开,通过TCP监听的方式组成集群进行处理。(因为fastcgi么有转发静态资源,所以需要nginx将静态资源转发对应的静态资源的目录)

php配置

18ea40f6106c2f4c50b32bc2403dee29.png

php-fpm 修改 php-fpm.conf

TCP模式:

listen = 0.0.0.0:9000

Unix domain socket模式:

listen = /opt/php/var/run/php-fpm.sock ; 这里的目录也可以选择/dev/shm/php-fpm.sock,/dev/shm/是一个内存的目录,相对读写的速度会快一点。

nginx修改

b21780804e2dc552e8ebffc23e42e96d.png

TCP模式:

upstream fpm_server{server 192.168.122.100:9000;server 192.168.122.101:9000;}server {listen 80;server_name localhost;root /var/www/php;location ~ [^/]\.php(/|$) {fastcgi_pass fpm_server;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}}

Unix domain socket模式:

server {listen 80;server_name localhost;root /var/www/php;location ~ [^/]\.php(/|$) {fastcgi_pass unix:/opt/php/var/run/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}}

总结

在服务器压力不大的情况下,tcp和Unix domain socket差别不大,但在压力比较满的时候,用套接字方式,效果比较好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值