V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
polandeme
V2EX  ›  程序员

求助 nginx 配置问题-FastCGI sent in stderr: "Primary script unknown"

  •  1
     
  •   polandeme ·
    polandeme · 2017-02-18 19:35:10 +08:00 · 22949 次点击
    这是一个创建于 2617 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个关于 nginx 配置的问题,配置一个 php 网站,页面总是输出 File not found.,查看 nginx log 中报错:

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

    nginx 配置如下:

    erver {
        listen 80;
        server_name abc.com;
        root "/root/work/abc/public";
    
        index index.html index.htm index.php;
    
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        access_log off;
        error_log  /var/log/nginx/abc.com-error.log error;
    
        sendfile off;
    
        client_max_body_size 100m;
    
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
            fastcgi_connect_timeout 300;
            fastcgi_send_timeout 300;
            fastcgi_read_timeout 300;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
    }
    

    查了一下,大多数给出的解决方法都是 fastcgi_param 改成 document_root ,但是我这个现在已经是这个了。

    还有一个怀疑是 php-fpm 的权限问题,但是不知道怎么解决和是否是这个问题:

    root     20618  0.0  0.4 317860 18816 ?        Ss   18:11   0:00 php-fpm: master process (/etc/php/5.6/fpm/php-fpm.conf)                      
    www-data 20620  0.0  0.1 317860  6828 ?        S    18:11   0:00 php-fpm: pool www                                                            
    www-data 20621  0.0  0.1 317860  6828 ?        S    18:11   0:00 php-fpm: pool www                                                            
    root     24811  0.0  0.0  11744   932 pts/7    S+   19:33   0:00 grep --color=auto php-fpm
    

    所以想请教一下有没有人遇到类似的问题,或者有没有解决方案?

    11 条回复    2021-03-22 11:56:56 +08:00
    taozywu
        1
    taozywu  
       2017-02-18 20:19:44 +08:00
    1.给我看下你 php-fpm 的配置
    2.请确定 /var/run/php/php5.6-fpm.sock 这个在 php-fpm 配置正确
    3.注意 php 和 nginx 执行权限
    polandeme
        2
    polandeme  
    OP
       2017-02-18 20:35:31 +08:00
    @taozywu
    php-fpm 配置是默认配置 [php-fpm.conf]( https://gist.github.com/polandeme/38bc11cc1b4aeebe65e0097a4a3ec1dc)
    /var/run/php/php5.6-fpm.sock 这个配置应该是正确的,存在这个文件,开始有报过这路径错误日志,我改过来了。
    现在是怀疑第三点,但是不知道怎么调试。
    zen9073
        3
    zen9073  
       2017-02-18 22:38:48 +08:00
    /etc/php/5.6/fpm/pool.d/www.conf 文件中 listen 字段是怎么配置的

    nginx 的运行用户和户组是什么

    另外你把 nginx 的 “ root ” 设置在 root 目录, nginx 或者 php-fpm 有权限读取吗
    julyclyde
        4
    julyclyde  
       2017-02-19 09:39:38 +08:00
    首先,既然 nginx 能收到 fastcgi 发来的错误信息,那就根本不需要考虑 nginx 和 php-fpm “能否通信”的问题了
    julyclyde
        5
    julyclyde  
       2017-02-19 09:41:48 +08:00
    我比较怀疑的是 fastcgi_split_path_info 的问题
    建议你用 socat 冒充 php 在那 unix socket 上监听,查一下 nginx 发来的各个 fastcgi_param 的值到底是什么;或用 strace 跟踪一下传递过来的字符串

    或者是去掉 fastcgi_split_path_info 试一下
    polandeme
        6
    polandeme  
    OP
       2017-02-19 23:20:35 +08:00
    @julyclyde
    @zen9073

    把项目目录移动到了 /var/www/ 中运行正常了,但是还是没有找到原因是什么。
    zen9073
        7
    zen9073  
       2017-02-20 07:44:40 +08:00
    @polandeme 如你所说那样就是因为 nginx 和 php-fpm 没有读取 /root 下文件的权限导致的问题。
    polandeme
        8
    polandeme  
    OP
       2017-02-20 10:25:26 +08:00
    @zen9073 我把 root 下项目对应的文件夹权限打开了,也不可以?
    julyclyde
        9
    julyclyde  
       2017-02-26 23:11:57 +08:00
    @polandeme 移除就修复了,说明你的 documen_root 有问题或者没权限
    不仅要“下对应的文件夹”还要“ root 本身”的整个目录树的权限都打开才可以
    feivorid
        10
    feivorid  
       2018-03-01 15:42:53 +08:00
    我一直在 var/www 目录下也没有好用 悲催
    yanglihui
        11
    yanglihui  
       2021-03-22 11:56:56 +08:00
    试试关闭 selinux

    (别问我怎么知道的,看到各种杂七杂八解决方案,看了一天了。。。没一个靠谱)

    查看 SELinux 状态:getenforce
    临时关闭:setenforce 0
    修改配置文件需要重启机器:/etc/selinux/config 将 SELINUX=enforcing 改为 SELINUX=disabled
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   924 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:15 · PVG 04:15 · LAX 13:15 · JFK 16:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.