为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Nginx + php-fpm 从特定 URI 提供 PHP 应用程序:“主脚本未知”错误

使用 Nginx + php-fpm 从特定 URI 提供 PHP 应用程序:“主脚本未知”错误

PHP
吃鸡游戏 2023-07-01 10:12:32
我正在尝试配置 Nginx 以服务 Django 应用程序和 Laravel 应用程序。我的 Django 应用程序正常工作,所以现在我想从 /snipe-it 获取 Laravel 应用程序。我正在努力解决的 nginx 配置的主要部分在这里:    location /snipe-it/ {        alias /var/www/html/public/;        #try_files $uri $uri/ /index.php$is_args$args;        location ~* \.php(/|$) {            try_files $uri $uri/ =404;            include fastcgi_params;            fastcgi_pass unix:/run/php/php7.3-fpm.sock;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        }    }该index.php文件的实际文件位置是/var/www/html/public/index.php。当尝试访问位于 的 Laravel 应用程序时http://127.0.0.1/snipe-it/index.php,我收到“文件未找到”。日志输出如下所示:nginx_1        | - -  24/Jun/2020:17:37:40 +0000 "GET /snipe-it/index.php" 404nginx_1        | 2020/06/24 17:37:40 [error] 17#17: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.30.0.1, server: 127.0.0.1, request: "GET /snipe-it/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.3-fpm.sock:", host: "127.0.0.1"nginx_1        | 172.30.0.1 - - [24/Jun/2020:17:37:40 +0000] "GET /snipe-it/index.php HTTP/1.1" 404 27 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0" "-"一切都在一个 docker 容器中,该容器源自使用 Debian buster 的官方 nginx 镜像。我相信日志中的第一行来自 php-fpm7.3 进程,因为我在文件中设置access.log了。我还希望它可以更深入地了解应用程序在何处查找文件,但似乎并非如此。/proc/self/fd/2/etc/php/7.3/fpm/pool.d/www.confcatch_workers_outputyesindex.php如果有帮助的话,权限index.php如下所示:-rw-r--r-- 1 docker www-data  1887 Jun 23 21:13 index.php我目前正在以 www-data 用户身份运行 nginx,以 www-data 组中的 docker 用户身份运行 php-fpm。似乎“主脚本未知”错误可能是由许多问题引起的,因此任何关于如何深入挖掘以找到有关问题所在的更多线索的提示都会有所帮助。仅供参考,这是我的完整 nginx conf 文件:upstream intranet {    server web:8000;}upstream prometheus {    server prometheus:9090;}
查看完整描述

1 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

根据 nginx ngx_http_fastcgi_module文档,$fastcgi_script_name内部变量包含请求 URI,或者,如果 URI 以斜杠结尾,则请求 URI 带有由附加到fastcgi_index其上的指令配置的索引文件名。当你使用

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

并收到/snipe-it/script.php传入请求,$uri内部变量等于 /snipe-it/script.php$document_root$fastcgi_script_name内部变量等于/var/www/html/public/,连接$document_root$fastcgi_script_name字符串变为/var/www/html/public//snipe-it/script.php,这显然会导致文件不存在。你能做的是

    location ~ ^/snipe-it/(?<subpath>.*)$ {
        alias /var/www/html/public/;
        location ~* \.php$ {
                    include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$subpath;
        }
    }

root请注意,当您使用指令而不是 时,不会出现此问题alias/var/www/html/public但是,除非您将目录重命名为/var/www/html/snipe-it(而不是可以使用) ,root /var/www/html;否则这是不可能的。alias /var/www/html/public/;


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信