不一样的nginx报错"Primary script unknown"

昨天晚上忘记对开发环境做了什么改动,导致今天来了在进行接口调试的时候nginx提示”Primary script unknown”,这个大多数情况下来说是一个很简单的问题:nginx配置里面的script_filename错误。然而我检查了我的nginx配置,发现并没有什么问题。

location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

同时,我也对这个配置进行了验证,在http模块的access日志格式配置里面加入请求文件名,并在sever模块里面使用这个配置

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" $document_uri $request_filename';
access_log  "logs/frontapi.access.log" main;

下面是打印的日志,经过验证,这个配置确实是没有问题的。

127.0.0.1 - - [18/Mar/2019:12:40:40 +0800] "GET /v1/product/search?category=2 HTTP/1.1" 404 27 "-" "PostmanRuntime/7.6.1" "-" /index.php /Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi/web/index.php
^C
xubanditdeMacBook-Pro:logs xubandit$ ll /Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi/web/index.php
-rwxr-xr-x  1 xubandit  staff  570  3  9 16:39 /Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi/web/index.php

然后又查了php代码,发现请求根本就没有到php代码这边来,也就是说问题出在了php-fpm,然后就想怎么对php-fpm进行调试,查看了php-fpm的日志,发现日志里面只记录了php-fpm master进程的信息,子进程里面进行php处理的内容是没有日志的。

只能进行进程调试了,然而发现macos下面并没有strace这个工具,好在有个替代品dtruss

xubanditdeMacBook-Pro:php-fpm.d xubandit$ sudo dtruss  -p 13735
dtrace: system integrity protection is on, some features will not be available

SYSCALL(args)          = return
poll(0x7FFEE3A437B0, 0x1, 0x1388)         = 1 0
getrusage(0x0, 0x7FFEE3A43670, 0x0)         = 0 0
getrusage(0xFFFFFFFFFFFFFFFF, 0x7FFEE3A43670, 0x0)         = 0 0
dtrace: error on enabled probe ID 2174 (ID 159: syscall::read:return): invalid kernel access in action #12 at DIF offset 68
dtrace: error on enabled probe ID 2174 (ID 159: syscall::read:return): invalid kernel access in action #12 at DIF offset 68
dtrace: error on enabled probe ID 2174 (ID 159: syscall::read:return): invalid kernel access in action #12 at DIF offset 68
dtrace: error on enabled probe ID 2174 (ID 159: syscall::read:return): invalid kernel access in action #12 at DIF offset 68
dtrace: error on enabled probe ID 2174 (ID 159: syscall::read:return): invalid kernel access in action #12 at DIF offset 68
lstat64("/Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi/web/index.php\0", 0x7FFEE3A52E30, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi/web\0", 0x7FFEE3A53980, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents/www/mmt/mmt-mall-php/frontapi\0", 0x7FFEE3A53980, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents/www/mmt/mmt-mall-php\0", 0x7FFEE3A53980, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents/www/mmt\0", 0x7FFEE3A53980, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents/www\0", 0x7FFEE3A53980, 0x0)         = -1 Err#13
stat64("/Users/xubandit/Documents\0", 0x7FFEE3A53980, 0x0)         = 0 0
stat64("/Users/xubandit\0", 0x7FFEE3A53980, 0x0)         = 0 0
stat64("/Users\0", 0x7FFEE3A53980, 0x0)         = 0 0
stat64("\0", 0x7FFEE3A53980, 0x0)         = -1 Err#2

通过调试发现php-fpm是正常接收到了文件这个参数,只是访问的时候报错了。然后检查配置文件发现php-fpm是nobody:nobody用户运行的,文件的owner是xubandit:staff,访问权限有问题,由于是我本地电脑,所以就把php-fpm运行的用户改成了xubandit:staff。问题到这里就解决了

tips:为了strace方便,我把php-fpm配置进行了修改,让php-fpm只fork出一个子进程处理所有的请求

pm.max_children = 1
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 1
    原文作者:bandit
    原文地址: https://segmentfault.com/a/1190000018561629
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞