【发布时间】:2018-09-19 08:58:36
【问题描述】:
我正在尝试翻转包含 Laravel 子应用程序的 PHP 应用程序的目录结构,该子应用程序当前具有以下目录结构:
/-
/directory1-
script1.php
/laravel-
/public-
/css-
some-file.css
... other laravel files ...
我现在要做的是将父目录中的“遗留”代码翻转到 Laravel 应用程序中,以便目录结构现在看起来像这样:
/-
/legacy-
/directory1-
script1.php
/public-
/css-
some-file.css
... other laravel files ...
我在配置 nginx 时遇到了困难,我能够成功运行 /legacy 脚本并在 Laravel 中加载静态公共文件(例如上面的 some-file.css),但我的 Laravel 路由都没有工作正常。这很复杂,因为我不希望更改任何旧路径(我仍然希望 / 转到现在的 /legacy/index.php)并且我希望 Laravel 应用程序以 /laravel 为前缀(因为我在 /laravel/api/v1 有调用 API 的前端代码,我还不想更新)。
以下是我对两个不同位置的 nginx 配置:
server {
# ...
root /var/www/legacy;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string;
}
location /laravel {
fastcgi_index index.php;
include fastcgi_params;
rewrite ^/laravel/public/(.*) /../public/$1;
try_files $uri /../public/index.php?$query_string;
}
}
我从 nginx 得到的错误是:FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,我认为这是在谈论 SCRIPT_FILENAME FastCGI 参数。我尝试为此添加一个命名位置,类似于this answer 中的方法:
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string @laravel;
}
location @laravel {
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php?$query_string;
include fastcgi_params;
}
但这具有相同的效果。如果有人有一个解决方案,能够将来自 /laravel/api/v1/.* 和 /api/v1/.* 的请求路由到 Laravel 应用程序,我们也将不胜感激。
【问题讨论】:
标签: laravel nginx fastcgi vhosts nginx-location