如何调试“FastCGI sent in stderr:Primary script unknown while reading response header from upstream”并找到实际的错误消息?

9wbgstp7  于 2022-10-22  发布在  PHP
关注(0)|答案(9)|浏览(351)

SO有许多文章提到此错误代码:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream...

这可能意味着这个错误消息或多或少是无用的。
消息告诉我们,FastCGI处理程序由于某种原因不喜欢发送的任何内容。问题是,有时我们不知道原因是什么。
所以我要重新陈述一个问题——我们如何***调试***这个错误代码?
考虑这样一种情况:我们有一个非常简单的站点,只有phpinfo。php文件。此外,还有一个非常简单的nginx配置,如下所示:

server {
    server_name testsite.local;

    root /var/local/mysite/;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  fastcgi_backend;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

我们如何准确地看到输出/日志fastcgi_params被发送到脚本
**如何查看实际的错误消息?**在我的例子中,我使用的是php-fpm。它在日志中没有关于此错误的信息。日志不会为此错误追加任何行。php-fpm有详细模式吗?

/var/log/php-fpm/error.log
/var/log/php-fpm/www-error.log

我已尝试在php-fpm中设置此项。conf文件

log_level = notice

这在php-fpm.d/www中。conf文件:

catch_workers_output = yes
ttcibm8c

ttcibm8c1#

要回答您的问题:

1.在php-fpm.d/www中。conf文件:
设置访问权限。日志条目:

access.log = /var/log/$pool.access.log

1.重启php-fpm服务。
1.尝试访问您的页面
1.cat/var/log/www.access。日志,您将看到如下访问日志:
- - 10/Nov/2016:19:02:11 +0000 "GET /app.php" 404 - - 10/Nov/2016:19:02:37 +0000 "GET /app.php" 404

要解决“主脚本未知”问题:

  • 如果您看到“GET/”没有正确的php文件名,那么这就是您的nginx conf问题。
  • 如果您看到404的“GET/app.php”,这意味着nginx正确地传递了脚本文件名,但php-fpm无法访问该文件(用户“php-fpm:php-fp”无法访问您的文件,这让我困了3个小时)

希望我的回答有帮助。

t0ybt7op

t0ybt7op2#

由于请求由php工作者处理,您可以跟踪php工作者以获得原因。
为了便于找到处理请求的工作进程,在php-fpm的conf文件中只设置一个工作进程。

pm.max_children = 1
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 1

使用$ps -aef | grep -v grep | grep php获取php工作pid

root     28879     1  0 Apr12 ?        00:00:02 php-fpm: master process (/etc/php-fpm.conf)
www      28880 28879  0 Apr12 ?        00:00:24 php-fpm: pool www

然后使用$ sudo strace -p 28880跟踪工作流程,然后执行请求,您将看到如下跟踪输出

strace: Process 28880 attached
accept(10,

{sa_family=AF_UNIX}, [112->2]) = 4
poll([{fd=4, events=POLLIN}], 1, 5000)  = 1 ([{fd=4, revents=POLLIN}])
times({tms_utime=1388, tms_stime=1099, tms_cutime=0, tms_cstime=0}) = 1336709044
read(4, "\1\1\0\1\0\10\0\0", 8)         = 8
read(4, "\0\1\0\0\0\0\0\0", 8)          = 8
read(4, "\1\4\0\1\4U\3\0", 8)           = 8
read(4, "\17DSCRIPT_FILENAME/data/HQ/SC_Edu"..., 1112) = 1112
read(4, "\1\4\0\1\0\0\0\0", 8)          = 8
lstat("/data/www/public/st/mn/dst.php", 0x7ffce98d7170) = -1 ENOENT (No such file or directory)
stat("/data/www/public/st/mn", 0x7ffce98d9580) = -1 ENOENT (No such file or directory)
stat("/data/www/public/st", 0x7ffce98d9580) = -1 ENOENT (No such file or directory)
stat("/data/www/public", {st_mode=S_IFDIR|0774, st_size=4096, ...}) = 0
...

从跟踪输出中,它显示脚本文件/data/www/public/st/mn/dst.php未退出

wlsrxk51

wlsrxk513#

对我来说,这是一个权限问题&我必须在php-fpm/www.conf中更改用户和组
将值更改为您的用户名和组www,例如

user = aqib
group = _www
zrfyljdw

zrfyljdw4#

Nginx的fastcgi_pass导致的问题不正确。
您可以检查以下内容:
打开文件:/etc/php-fpm.confsystemctl status php-fpm查找它)并在=之后找到pid =。这是一个路径,需要与文件/etc/php-fpm.d/www.conf中的路径listen =进行比较。
例如:
pid = /run/php-fpm/php-fpm.pid
listen = /run/php-fpm/php-fpm.sock;
我的旧路径listen = /var/run/php-fpm/php-fpm.sock;和我更改为/run/php-fpm/php-fpm.sock;,然后重新启动php-fpm,然后它运行良好。

zdwk9cvp

zdwk9cvp5#

在我的示例中,发生此错误是因为php-fpm无法根据$document_root(即/etc/nginx/html)找到文件
通过设置

location ~ \.php$ {
        root /var/www/html;
        fastcgi_index   index.php;
        ...
    }

PHP-FPM现在可以正确定位文件。

xpszyzbs

xpszyzbs6#

检查root的位置以及它下面的文件是否存在,我遇到这个错误是因为root路径TYPO

wf82jlnq

wf82jlnq7#

对于我(Oracle Linux 7)来说,正是SELinux阻止了php-fpm访问该文件。Red Hat和Centos用户可能会发现同样的问题。
运行,运行

sestatus

显示了SELinux的状态,对我来说,它是打开的,导致了问题。我已经尝试并实现了所有其他建议的修复,但错误仍然存在。
我通过将SELinux置于许可模式来解决这个问题:
/etc/selinux/config


# cat /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

# enforcing - SELinux security policy is enforced.

# permissive - SELinux prints warnings instead of enforcing.

# disabled - No SELinux policy is loaded.

SELINUX=permissive

# SELINUXTYPE= can take one of these two values:

# targeted - Targeted processes are protected,

# minimum - Modification of targeted policy. Only selected processes are protected.

# mls - Multi Level Security protection.

SELINUXTYPE=targeted

我的最小配置的其余部分:/etc/nginx/nginx.conf:

user  apache;
worker_processes  1;

events {
}
http {

        include mime.types;
        server {
                listen 80;
                server_name 111.222.111.222; #your ip address
                root /sites/demo;

                index index.php index.html;

                location ~ \.php$ {
                        # pass to php-fpm
                        include fastcgi.conf;
                        include fastcgi_params;
                        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
                }

        }
}

注意:我将用户更改为apache,这是php-fpm使用的用户
/etc/php-fpm.d/www.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 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 = /run/php-fpm/php-fpm.sock

; Set listen(2) backlog.
; Default Value: 511
;listen.backlog = 511

; 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.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = apache
listen.group = apache
listen.mode = 0660

注:我设置了listen。主人并倾听。组到apache
我的文件和文件夹的权限是:/sites(root:apache drwxrwxr-x)/sites/demo(root:apache drw xrwx-x)/site/demo/index。php(根:apache.rw.rw.r.)
如果您查看ps-aux|grep-E“php|nginx”,您可以看到所有php-fpm池进程和nginx工作进程都以用户apache身份运行。
如果您查看套接字文件/run/php-fpm/php-fdm的权限。sock你会看到它属于apache:apache和srw-rw----

uwopmtnx

uwopmtnx8#

在我的案例中,一开始我就开始与数百万个代码问题作斗争,但后来我看到了全球的大局。问题在于PHP的错误版本。最终,我一个接一个地尝试了不同的PHP版本,如下所示:

fastcgi_pass unix:/run/php/php7.3-fpm.sock

fastcgi_pass unix:/run/php/php5.3-fpm.sock

然后它工作了,没有真正地重新开发和更改数百万行代码。我知道这有点傻,但如果用旧风格和旧PHP方式开发,新的PHP版本绝对会崩溃。对我来说,运行它比运行最新最新的PHP版本更重要。

x33g5p2x

x33g5p2x9#

对于MacO用户,如果有人遇到我的情况:
我用以下方式启动了php服务:

sudo brew start php72

因为我使用了“sudo”权限。我需要在没有sudo的情况下停止和启动php服务。

sudo brew stop php72
brew start php72

希望对某人有帮助。

相关问题