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

如何调试“FastCGI sent in stderr: Primary script unknown while reading response header from upstream

问题描述:

SO 有很多文章提到了这个错误代码:

SO has many articles mentioning this error code:

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

这可能意味着此错误消息或多或少是无用的.

That probably means that this error message is more or less useless.

该消息告诉我们,由于某种原因,FastCGI 处理程序不喜欢它发送的任何内容.问题是有时我们不知道原因是什么.

The message is telling us that the FastCGI handler doesn't like whatever it was sent for some reason. The problem is that sometimes we have no idea what the reason is.

所以我要重申这个问题——我们如何调试这个错误代码?

So I'm re-stating the question -- How do we debug this error code?

考虑我们有一个非常简单的站点,只有 phpinfo.php 文件的情况.另外还有一个非常简单的nginx配置,如下:

Consider the situation where we have a very simple site, with just the phpinfo.php file. Additionally, there is a very simple nginx config, as follows:

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 有详细模式吗?

How can we see the actual error message? In my case, I'm using php-fpm. It has no info in the log about this error. The logs do not append any rows for this error. Is there a verbose mode for php-fpm?

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

我已尝试在 php-fpm.conf 文件中进行设置

I've tried to set this in the php-fpm.conf file

log_level = notice

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

and this in the php-fpm.d/www.conf file:

catch_workers_output = yes

回答您的问题:

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

设置 access.log 条目:

set the access.log entry:

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

  1. 重启 php-fpm 服务.

  1. restart php-fpm service.

尝试访问您的页面

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

cat /var/log/www.access.log, you will see access logs like:

- - 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配置问题.

  • if you see "GET /" without a correct php file name, then it's your nginx conf problem.

如果您看到带有 404 的GET/app.php",则表示 nginx 正确传递了脚本文件名,但 php-fpm 无法访问此文件(用户php-fpm:php-fpm"不无法访问您的文件,这让我困了 3 小时)

if you see "GET /app.php" with 404, it means nginx is correctly passing the script file name but php-fpm failed to access this file (user "php-fpm:php-fpm" don't have access to your file, which trapped me for 3 hours)

希望我的回答有帮助.