Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

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;
    }
}

How can we see output/log exactly what fastcgi_params got sent to the script?

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

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

log_level = notice

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

catch_workers_output = yes
like image 382
101010 Avatar asked Feb 08 '16 03:02

101010


3 Answers

To answer your question:

  1. in php-fpm.d/www.conf file:

set the access.log entry:

access.log = /var/log/$pool.access.log
  1. restart php-fpm service.

  2. try to access your page

  3. 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

To resolve "Primary script unknown" problem:

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

  • 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)

Hope my answer helps.

like image 100
Shengjie YU Avatar answered Sep 22 '22 05:09

Shengjie YU


As request is processed by the php worker, you can trace the php worker to get the reason.

To make it easy to locate which worker process the request, set only one worker in php-fpm's conf file.

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

Get php work pid with $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

Then trace the work process with $ sudo strace -p 28880, and then do the request, and you will see the trace output like below

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
...

From the trace output, it shows script file /data/www/public/st/mn/dst.php not exit

like image 23
LF00 Avatar answered Sep 24 '22 05:09

LF00


For me it was a permission issue & I had to change user & group in php-fpm/www.conf

change values to your username & group _www e.g

user = aqib
group = _www
like image 30
Aqib Avatar answered Sep 25 '22 05:09

Aqib