当前位置:首页 > 计算机科学 > 网络技术

nginx与php-fpm通信的两种方式

fanglong6年前 (2020-08-05)网络技术2298

在linux中,nginx服务器和php-fpm可以通过tcp socket和unix socket两种方式实现。

unix socket是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。这种方式需要再nginx配置文件中填写php-fpm的pid文件位置,效率要比tcp socket高。

tcp socket的优点是可以跨服务器,当nginx和php-fpm不在同一台机器上时,只能使用这种方式。

  • windows系统只能使用tcp socket的通信方式
    配置方法

  1. tcp socket:tcp socket通信方式,需要在nginx配置文件中填写php-fpm运行的ip地址和端口号。

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
}
  1. unix socket:unix socket通信方式,需要在nginx配置文件中填写php-fpm运行的pid文件地址。

//service php-fpm start生成.sock文件
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;
}

php-fpm的运行端口号和socket文件的地址都是在php-fpm.conf中配置的。
php-fpm.conf文件在php安装文件的/etc目录下,
比如你的php安装在/opt/php目录,则应该是/opt/php/php-fpm.conf。

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
;                            specific port;
;   '[::]:port'            - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock

通过注释可以看到,php-fpm的listen指令可以通过五种方式处理FastCGI请求,分别是:

  1. ipv4:端口号

  2. ipv6:端口号

  3. port相当于 0.0.0.0:port,本机所有ipv4对应的端口号

  4. unix socket文件

直接配置使用unix socket文件之后,会遇到access deny的问题,由于socket文件本质上还是一个文件,存在权限控制问题,默认由root用户创建,因此nginx进程无权限访问,应该配置如下命令:

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = www
listen.group = www 
listen.mode = 0660

可以配置nginx和php-fpm都是用www用户,这样就不会存在权限问题,当然也可以创建不同的用户,然后加入同一个组,便于分配权限。


扫描二维码推送至手机访问。

版权声明:本文由人从众的博客发布,如需转载请注明出处。

本文链接:https://loulin.bid/11.html

分享给朋友:

相关文章

linux服务器上thinkphp等系统系统登录验证码一直提示不正确

用phpinfo函数找到相应的有效php.ini文件修改如下段落:session.save_path="/tmp"去掉;然后chmod -R 777 /tmp (赋予权限)重启相应…

Linux下的tmpfs文件系统(/dev/shm)

介绍/dev/shm/是一个使用就是tmpfs文件系统的设备,其实就是一个特殊的文件系统。centos中默认大小为物理内存的一半,使用时不用mkfs格式化。tmpfs是Linux/Unix系统上的一种…

centos7编译BBRplus最新内核_不断更新中

bbrplus4.14.195最新内核下载:https://dl.loulin.bid:89/linux/centos/RPMS/kernel/el7/bbrplus/ headers de…

个人建站技巧

现在有不少人在问我,怎样才能建设一个网站啊,建设一个网站需要学习什么东西,建站需要怎么设计,怎样设计和制作自己的个人网站啊,个人网站需要什么内容啊,个人网站的路在何方啊,怎样盈利啊等等。我也是做了几年…

centos7中gcc 10的安装

centos7中gcc 10的安装

1.全编译安装(截止20200902最新版比较适合大陆自行搭建测试)# 如果内存偏小的比如4G以下的,加大交换分区容量,查看 http://loulin.bid/9.html #…

评论列表

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。