【问题标题】:Nginx + PHP-FPM redirect to static PHP fileNginx + PHP-FPM 重定向到静态 PHP 文件
【发布时间】:2018-10-30 06:20:24
【问题描述】:

关于我的设置的一些细节:

  • 我正在从默认 Nginx webroot 提供静态 web 应用程序 (HTML + JS)
  • 我有一个 PHP-FPM 服务器在 localhost:9000 上运行
  • FPM 的目标文件应该是 /api/webroot/index.php(总是,不需要 try_files 等)
  • 我需要转发所有 /api 和 /api-debug 调用以到达 localhost:9000,并且 /app/webroot/index.php 应该处理所有这些请求。

我有以下 工作 Nginx 配置:

upstream fastcgi_backend {
    server localhost:9000;
    keepalive 30;
}

server {
    listen   80;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        location ~ ^/(api|api-debug)/ {
            root       /app/webroot;
            index      index.php;
            try_files  $uri /api/index.php$is_args$args;

            location ~ \.php$ {
                fastcgi_pass   fastcgi_backend;

                fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*)$;
                fastcgi_param  SCRIPT_FILENAME /app/webroot/$fastcgi_script_name;

                include        fastcgi_params;
            }
        }
    }
}

我只是想让它更简单、更高效,因为在我现在看来,它是一团糟。 例如,我试图调整

try_files $uri /api/index.php$is_args$args;

到

try_files $uri /api/webroot/index.php$is_args$args;

它失败了......它起作用的唯一原因是/api/index.php包含/api/webroot/index.php,但我认为它效率低下。

我发现调试 nginx 配置很难,因为它不容易测试。

非常感谢您提前提供的帮助!

【问题讨论】:

  • 根据您的问题,index.php 位于/app/webroot/index.php。但根据您的working 配置文件,URI /api/index.php 将解析为/app/webroot/api/index.php 的路径名。将webroot 添加到 URI 没有意义,您到底想实现什么?
  • 嗨,理查德,感谢您的回复。这两个文件都存在:/app/index.php 和 /app/webroot/index.php,其中 /app/index.php 只是 /app/webroot/index.php 的包含语句。请注意,这些文件位于磁盘上的 /app/xxx 和 NOT 的 /api/xxx。 /api/ 只是 URL。
  • 最终目标是让它工作:CakePHP docs 使用以下内容: - 重写规则仅适用于以 /api 开头的 URL-s - 文件在 nginx 服务器的主机上不可用( try_files 将失败,我也看不出有任何需要,只需以正确的重写形式将所有 /api 请求传递给 localhost:9000/app/webroot/index.php )希望我在这里做了一些澄清。谢谢你的帮助! @RichardSmith
  • 好的,我没有注意到 fastcgi_split_path_info 从 URI 中删除了 /api/ 前缀。

标签: php nginx static fpm


【解决方案1】:

最简单的解决方案是将 SCRIPT_FILENAME 硬连线为 /app/webroot/index.php 并完全删除您的 location 块之一。

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

location ~ ^/(api|api-debug)/ {
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME /app/webroot/index.php;
    fastcgi_pass   fastcgi_backend;
}

或者,为了保持指定带有 .php 扩展名的 URI 的灵活性,您可以通过以下方式简化配置:

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;

    rewrite ^/(api|api-debug)/ /index.php last;
}

location ~ \.php$ {
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME /app/webroot$uri;
    fastcgi_pass   fastcgi_backend;
}

【讨论】:

  • 谢谢理查德,我明天试试这个并报告!
  • 嗨,理查德,对 /api 的调用最终在 PHP-FPM 上正确结束,但我有一些重写问题,我粘贴了 print_r($_SERVER); 的差异; here。你的配置结果在左边,我的工作配置在右边。我唯一的问题是我不知道它是如何工作的,而且我不喜欢那样的东西......我应该用 fastcgi_param 硬连线替换所有变量吗?还是有更清洁的解决方案?谢谢!
  • 在上面的例子中,URI 没有被重写,所以所有的 FastCGI 参数都派生自原始请求。此外,root 不在范围内,因此包含nginx 默认值。所有这些参数都在fastcgi_params 文件中定义,因此您可以使用应用程序需要的内容覆盖它们,或使用底部示例。
  • 亲爱的理查德,经过几个小时的调试,我找到了应用程序真正需要的东西。我之前没有详细注意这一点,因为我使用的是带有框架附带的预制 .htaccess 文件的 apache。所以事实证明 /api/some/path/here?sample=querystring 应该像这样结束:/app/webroot/index.php/api/some/path/here?sample=querystring,并且没有其他 .php 文件将永远被处理,所以不需要第二个“位置〜\ .php $”块(我猜?)。你能帮我把它做得尽可能干净和高效吗?非常感谢您的慷慨帮助!
  • 抱歉,我忘了这也适用于 /api-debug...再次感谢您!
猜你喜欢
  • 1970-01-01
  • 2014-08-04
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多