【发布时间】: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/前缀。