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


    这篇文章主要介绍了php 与 nginx 的两种处理方式及nginx与php-fpm通信的两种方式,需要的朋友可以参考下

     

    先给大家介绍下php 与 nginx 的两种处理方式,具体内容如下所示:

    1.IP:Port 监听方式

    1
    2
    3
    4
    php-fpm
    docker pull PHP:2.4-alpine
    nginx.conf
    fastcgi_pass 127.0.0.1:9000;

    php-fpm 在容器里的 nginx.conf

    1
    2
    3
    4
    5
    location /php
        {    
          proxy_set_header Host $host:$server_port;
          proxy_pass http://138.38.38.111:80/;
        }

    2.UDS 方式监听

    1
    2
    3
    4
    php-fpm
    listen = /tmp/php-fpm.sock
    nginx.conf
    fastcgi_pass unix:/tmp/php-fpm.sock;

    3.注意

    php-fpm用ip:port方式建立链接,

    nginx不要用unix socket方式建立链接,用ip:port方式建立连接就行

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

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

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

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

    windows系统只能使用tcp socket的通信方式

    配置方法

    tcp socket

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

    1
    2
    3
    4
    5
    6
    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;
    }

    unix socket

    unix socket通信方式,需要在nginx配置文件中填写php-fpm运行的pid文件地址。

    1
    2
    3
    4
    5
    6
    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。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ; 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. [::]:port,包括ipv4和ipv6  
    5. unix socket文件

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

    1
    2
    3
    4
    5
    6
    7
    8
    ; 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用户,这样就不会存在权限问题,当然也可以创建不同的用户,然后加入同一个组,便于分配权限。

    总结

    以上所述是小编给大家介绍的php 与 nginx 的两种处理方式及nginx与php-fpm通信的两种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持

  • 相关阅读:
    Struts2学习总结——文件上传与下载
    String.StartsWith与 EndsWith在大量使用时效率果然极低
    使用CXF实现基于Rest方式的WebService
    使用CXF实现基于Soap协议的WebService
    Hibernate单向“一对多”关联
    Hibernate单向“多对一”关联
    Hibernate单向“一对一”关联
    Spring使用经验之Listener综述
    Eclipse3.4以上使用dropins的插件安装方式
    数据库第一、二、三范式
  • 原文地址:https://www.cnblogs.com/brady-wang/p/10067447.html
Copyright © 2020-2023  润新知