简而言之,我正在尝试让 PHP 脚本监听 unix 套接字上的请求,并向其发送来自另一个 PHP 脚本的请求。 我已经这样配置 PHP-FPM:
[a]
; Unix user/group of processes
user = www
group = www
listen = /var/run/php-fpm-a.sock
;listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0660
; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 75
pm.start_servers = 3
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 500
; host-specific php ini settings here
php_admin_value[open_basedir] = /usr/local/www/a
php_flag[display_errors] = on
/usr/local/www/a contains the following index.php:
<?php
echo 'test\ntest\ntest\n';
还有另一个 PHP-FPM 配置文件可以有效地监听 /var/run/php-fpm-b.sock
并且 Nginx 指向它(这个位工作正常),其中包含以下内容/usr/local/www/b/index.php
中的代码:
echo 'TEST B';
$fp = fsockopen('unix:///var/run/php-fpm-a.sock', -1, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /index.php HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
显然我在/usr/local/www/b/index.php 中有问题,因为我得到的只是“TEST B”作为输出。我不认为这是一个套接字权限问题,因为它会指出一个错误,我的猜测是 $out
对此工作是错误的,但不知道 PHP 除了接收什么之外。任何帮助将不胜感激。
注意:在 FreeBSD11 上使用 PHP7
最佳答案
PHP-FPM是一个 FastCGI 进程管理器。 FastCGI和 HTTP是两个不同的协议(protocol)。因此,PHP-FPM 无法直接使用 HTTP。
browser -> (HTTP) -> nginx -> (FastCGI) -> PHP-FPM + scriptB
scriptB -> (HTTP) -> PHP-FPM + scriptA
您有两个选择:
- 将
脚本A
放在nginx后面,然后打开TCP套接字而不是unix套接字从B到A进行通信。 - 修改
脚本 B
以使用FastCGI
语言使用脚本 A
而不是HTTP
.
关于PHP监听unix套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743431/