AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 517190
Accepted
We0
We0
Asked: 2013-06-21 00:33:56 +0800 CST2013-06-21 00:33:56 +0800 CST 2013-06-21 00:33:56 +0800 CST

Nginx 1 FastCGI 在 stderr 中发送:“主脚本未知”

  • 772

我第一次使用 Nginx,但我对 Apache 和 Linux 非常熟悉。我正在使用现有项目,每当我尝试查看 index.php 时,都会收到 404 文件未找到的消息。

这是 access.log 条目:

2013/06/19 16:23:23 [error] 2216#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.ordercloud.lh"

这是站点可用文件:

server {
    set $host_path "/home/willem/git/console/www";
    access_log  /www/logs/console-access.log  main;

    server_name  console.ordercloud;
    root   $host_path/htdocs;
    set $yii_bootstrap "index.php";

    charset utf-8;

    location / {
        index  index.html $yii_bootstrap;
        try_files $uri $uri/ /$yii_bootstrap?$args;
    }

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

    #avoid processing of calls to unexisting static files by yii
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php {
        fastcgi_split_path_info  ^(.+\.php)(.*)$;

        #let yii catch the calls to unexising PHP files
        set $fsn /$yii_bootstrap;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

        #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
    }

    location ~ /\.ht {
        deny  all;
    }
}

我的 /home/willem/git/console 归 www-data:www-data 所有(我的网络用户运行 php 等),出于沮丧我给了它 777 权限...

我最好的猜测是配置有问题,但我无法弄清楚......

更新 所以我把它移到/var/www/并使用了一个更基本的配置:

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www/;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name console.ordercloud;

    location / {
        root           /var/www/console/frontend/www/;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www;
            include        fastcgi_params;
    }

    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
            try_files $uri =404;
        }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

}

另外,如果我打电话给localhost/console/frontend/www/index.php我,我会得到 500 PHP,这意味着它在那里服务。它只是不服务于 console.ordercloud ...

nginx
  • 15 15 个回答
  • 412110 Views

15 个回答

  • Voted
  1. Fleshgrinder
    2013-06-21T08:50:46+08:002013-06-21T08:50:46+08:00

    错误消息“未知主脚本”几乎总是SCRIPT_FILENAME与nginx 指令中的错误设置有关fastcgi_param(或不正确的权限,请参阅其他答案)。

    您在if首先发布的配置中使用了 。那么现在应该众所周知,if 是邪恶的并且经常产生问题。

    在 location 块中设置root指令是不好的做法,当然它有效。

    您可以尝试以下操作:

    server {
        location / {
            location ~* \.php$ {
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;
                try_files $uri @yii =404;
            }
        }
        location @yii {
            fastcgi_param SCRIPT_FILENAME $document_root$yii_bootstrap;
        }
    }
    

    请注意,以上配置未经测试。您应该nginx -t在应用它之前执行以检查 nginx 可以立即检测到的问题。

    • 125
  2. William Turrell
    2016-02-06T16:11:36+08:002016-02-06T16:11:36+08:00

    这并不总是SCRIPT_FILENAME错误的。
    也可能是PHP 以错误的用户/组运行。

    这个例子是针对Mac OS X的,根据我的经验,这是最麻烦的设置(相比之下 Debian 很容易)——我刚刚从 PHP 5.6 升级到 7.0,使用自制软件和优秀的 josegonzalez 包。

    问题是创建了配置文件的新副本。

    主配置文件是/usr/local/etc/php/7.0/php-fpm.conf,但请注意 末尾的Pool Definitions部分,它包含一个完整的子目录。

    include=/usr/local/etc/php/7.0/php-fpm.d/*.conf

    里面有php-fpm.d一个www.conf文件。默认情况下,它具有:

    user = _www
    group = _www
    

    在 OS X 上,您可能需要将其更改为:

    user = [your username]
    group = staff
    

    (您应该会发现这与ls -lh您的 document_root 匹配)

    不幸的是,如果没有这个改变,你仍然会在你的 Nginx 错误日志中看到这个,即使它在正确的地方寻找文件。

    "Primary script unknown" while reading response header from upstream
    

    验证它当前运行的是什么:

    ps aux | grep 'php-fpm'
    

    或更干净:

    ps aux | grep -v root | grep php-fpm | cut -d\  -f1 | sort | uniq
    

    如何验证脚本文件名是否正确:

    (从另一个答案中的 igorsantos07 偷来的)

    添加到httpmain 块/usr/local/etc/nginx/nginx.conf:

    log_format scripts '$document_root$fastcgi_script_name > $request';
    

    (第一位需要是你当前使用的任何东西,所以你可以看看它是否正确。)

    server并在您网站的区块中使用您刚刚定义的日志:

    access_log /var/log/nginx/scripts.log scripts;
    

    如果正确,请求 example.com/phpinfo.php 将产生如下内容:

    /path/to/docroot/phpinfo.php > GET /phpinfo.php
    

    你能简化你现有的配置吗?

    您使用的是location ~ \.php {从互联网以外的某个地方复制/粘贴的块吗?大多数软件包允许您更快、更干净地完成它。例如在 OS X 上你现在只需要这个:

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        include snippets/fastcgi-php.conf;
    
        # any site specific settings, e.g. environment variables
    }
    

    fastcgi_split_path_info、try_files 和 fastcgi_index(默认为 index.php)之类的东西在/usr/local/etc/nginx/snippets/fastcgi-php.conf.

    这又包括/usr/local/etc/nginx/fastcgi.conf一个fastcgi_param设置列表,包括关键的 SCRIPT_FILENAME。

    永远不要root在 PHP 位置块中重复。

    • 66
  3. Best Answer
    We0
    2013-06-21T02:00:38+08:002013-06-21T02:00:38+08:00

    好的,经过一天的努力,我发现了 3 件事

    1. 出于某种原因,我已经在端口 9000 上运行了一些东西,所以我改为 9001
    2. 我的默认网站拦截了我的新网站,我又一次不明白为什么,因为它不应该,但我只是取消了它的链接
    3. Nginx 不会自动为站点可用的站点启用符号链接。

    希望这可以节省一些麻烦!

    • 9
  4. Dan Dascalescu
    2015-06-18T01:24:31+08:002015-06-18T01:24:31+08:00

    较新的 nginx (v1.8) 也有同样的问题。较新的版本建议使用snippets/fastcgi-php.conf;而不是fastcgi.conf. 因此,如果您include fastcgi.conf从教程中复制/粘贴,最终可能会Primary script unknown在日志中出现错误。

    • 8
  5. PLA
    2017-07-28T07:05:00+08:002017-07-28T07:05:00+08:00

    “未知主脚本”是由SELinux 安全上下文引起的。

    客户端得到响应

    文件未找到。

    nginx error.log 有以下错误信息

    *19 FastCGI 在 stderr 中发送:“Primary script unknown”同时从上游读取响应标头

    所以只需将 Web 根文件夹的安全上下文类型更改为httpd_sys_content_t

    chcon -R -t httpd_sys_content_t /var/www/show
    




    nginx/php-fpm 配置有 3 个用户

    /etc/nginx/nginx.conf

    user nobody nobody;  ### `user-1`, this is the user run nginx woker process
    ...
    include servers/*.conf;
    

    /etc/nginx/conf.d/www.conf

    location ~ \.php$ {
    #   fastcgi_pass 127.0.0.1:9000;  # tcp socket
        fastcgi_pass unix:/var/run/php-fpm/fpm-www.sock;  # unix socket
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    

    /etc/php-fpm.d/www.conf

    [www]
    user = apache  ### `user-2`, this is the user run php-fpm pool process
    group = apache
    
    ;listen = 127.0.0.1:9000  # tcp socket
    listen = /var/run/php-fpm/fpm-www.sock  # unix socket
    
    listen.onwer = nobody  ### `user-3`, this is the user for unix socket, like /var/run/php-fpm/fpm-www.sock
    listen.group = nobody  # for tcp socket, these lines can be commented
    listen.mode = 0660
    

    user-1 和 user-2 不必相同。

    对于 unix 套接字,user-1 需要与 user-3 相同, 因为 nginx fastcgi_pass 必须对 unix 套接字具有读/写权限。

    否则 nginx 会得到502 Bad Gateway,并且 nginx error.log 有以下错误信息

    *36 connect() to unix:/var/run/php-fpm/fpm-www.sock 在连接到上游时失败(13:权限被拒绝)

    并且 web 根文件夹 (/var/www/show) 的用户/组不必与这 3 个用户中的任何一个相同。

    • 6
  6. Seb35
    2015-08-06T11:39:43+08:002015-08-06T11:39:43+08:00

    我也有这个问题,我通过交换行include fastcgi_params和解决了它fastcgi_param SCRIPT_FILENAME ...。

    事实上,nginx 设置了每个 FastCGI 参数的最后一个值,因此您必须将您的值放在 fastcgi_params 中包含的默认值之后。

    • 2
  7. Kent
    2019-03-14T01:01:49+08:002019-03-14T01:01:49+08:00

    I solved this problem by closing SELINUX in CentOS7.3 system

    steps:

    • exec setenforce 0
    • U also need to modify config file

    vim /etc/selinux/config set SELINUX to disabled

    • 2
  8. spetsnaz
    2018-08-13T09:38:38+08:002018-08-13T09:38:38+08:00

    检查您的 php-fpm sock 文件的权限,不知何故无法访问:

    chmod 755 /usr/local/var/run/php-fpm.sock

    然后尝试重启nginx。

    • 1
  9. lg.
    2013-06-21T01:28:10+08:002013-06-21T01:28:10+08:00

    尝试在您的 php 位置添加 root 指令。

    location ~ \.php {
          root /home/willem/git/console/www;
          ...
    }
    
    • 0
  10. Christian
    2016-08-05T03:57:18+08:002016-08-05T03:57:18+08:00

    我发现您的问题是在寻找相同的错误消息,但使用的是 apache + php-fpm(无 nginx)。对我来说,问题是错误位置的斜线:许多设置建议包括以下形式的一行:

    SetHandler "proxy:unix:/path/to/file.socket|fcgi://localhost/:9000"
    

    通过将最后一个斜杠放在端口号之后,如下所示:

    SetHandler "proxy:unix:/path/to/file.socket|fcgi://localhost:9000/"
    

    这个问题对我来说消失了。也许你可以做类似的事情

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve