繁体   English   中英

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

[英]How to debug "FastCGI sent in stderr: Primary script unknown while reading response header from upstream" and find the actual error message?

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

回答你的问题:

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

设置 access.log 条目:

access.log = /var/log/$pool.access.log
  1. 重启 php-fpm 服务。

  2. 尝试访问您的页面

  3. cat /var/log/www.access.log,你会看到如下访问日志:

- - 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-fpm”无权访问您的文件,这让我困了 3 个小时)

希望我的回答有帮助。

对我来说,这是一个权限问题,我必须在php-fpm/www.conf更改用户和组

将值更改为您的用户名和组 _www 例如

user = aqib
group = _www

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

由于请求是由 php worker 处理的,您可以跟踪 php worker 以获取原因。

为了便于定位处理请求的 worker,在 php-fpm 的 conf 文件中只设置一个 worker。

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 $ps -aef | grep -v grep | grep php $ps -aef | grep -v grep | grep php

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 not exit

对我来说(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.owner 和 listen.group 设置为 apache

我的文件和文件夹的权限是:/sites (root:apache drwxrwxr-x) /sites/demo (root:apache drwxrwxr-x) /sites/demo/index.php (root:apache .rw.rw.r. .)

如果你看看 ps -aux | grep -E "php|nginx" 你可以看到所有的 php-fpm 池进程和 nginx 工作进程都以用户 apache 运行。

如果您查看套接字文件 /run/php-fpm/php-fpm.sock 的权限,您将看到它归 apache:apache 和 srw-rw----

Nginx 的 fastcgi_pass 不正确导致的问题。

您可以检查以下内容:

打开文件: /etc/php-fpm.confsystemctl status php-fpm找到它)并找到pid = after = 是你需要与你的路径listen =在文件/etc/php-fpm.d/www.conf比较的路径/etc/php-fpm.d/www.conf 2 路径必须到相同的位置。

例如:

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 然后它运行良好。

就我而言,发生此错误是因为 php-fpm 无法根据$document_root找到文件,即/etc/nginx/html

通过设置

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

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

对于 MacOs 用户,如果有人遇到我的情况:

我已经开始了 php 服务:

sudo brew start php72

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

sudo brew stop php72
brew start php72

希望对某人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM