首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Nginx + PHP -fpm从特定URI服务php应用程序:“主脚本未知”错误

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

Stack Overflow用户
提问于 2020-06-25 02:33:26
回答 1查看 513关注 0票数 0

我正在尝试将Nginx配置为同时服务于Django应用程序和Laravel应用程序。我已经让Django应用程序正常工作了,所以现在我想让/snipe-it为Laravel应用程序提供服务。

我正在努力处理的nginx配置的主要部分如下:

代码语言:javascript
复制
    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

在尝试访问http://127.0.0.1/snipe-it/index.php上的Laravel应用程序时,我收到“找不到文件”的提示。日志输出如下所示:

代码语言:javascript
复制
nginx_1        | - -  24/Jun/2020:17:37:40 +0000 "GET /snipe-it/index.php" 404
nginx_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进程,因为我在/etc/php/7.3/fpm/pool.d/www.conf文件中将access.log设置为/proc/self/fd/2。我还将catch_workers_output设置为yes,希望它能更深入地了解应用程序在何处查找index.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文件:

代码语言:javascript
复制
upstream intranet {
    server web:8000;
}

upstream prometheus {
    server prometheus:9090;
}

server {
    listen 80;
    server_name 127.0.0.1;
    root /var/www/html/public;
    index index.php index.html index.htm;

    location /static {
        alias /home/static;
    }

    location /media {
        alias /home/media;
    }

    # Prometheus settings
    location /prometheus/ {
        proxy_pass http://prometheus;
    }

    location /snipe-it/ {
        alias /var/www/html/public/;

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

    location / {
        proxy_pass http://intranet;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-25 03:23:05

根据nginx ngx_http_fastcgi_module documentation的说法,$fastcgi_script_name内部变量包含请求URI,或者,如果URI以斜杠结尾,则请求URI带有附加了fastcgi_index指令的索引文件名。当您使用

代码语言:javascript
复制
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,这显然会导致文件缺失。你能做的是

代码语言:javascript
复制
    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/;)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62561867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档