gpt4 book ai didi

php - 如何在 nginx 的子目录中获取 Slim PHP Framework 路由

转载 作者:可可西里 更新时间:2023-10-31 23:56:52 24 4
gpt4 key购买 nike

我正在尝试将一个 Slim 应用程序移动到一个子目录,以便可以在 example.com/api/ 访问它,但是我在让路由正常工作时遇到了严重的问题。

主要脚本位于 /website/workbench/api/public/index.php,因此调用 example.com/api/project/1 应该命中API文件夹。但是,我还需要能够访问 example.com 的 index.html 文件(在 Angular JS 上运行)。

当我转到 example.com/api/project/1 时,它确实命中了 PHP 脚本 - 我可以 var_dump 变量并查看它们。但是路由没有生效,请求变量好像是空的。

/etc/nginx/sites-available/workbench

server {
listen 80; ## listen for ipv4; this line is default and implied

root /website/workbench;
index index.php index.html index.htm;

server_name example.com;

location / {
try_files $uri $uri/ index.php?$query_string;
}

location /api/ {
try_files $uri $uri/ /api/public/index.php?$query_string;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param PHP_VALUE "newrelic.appname=workbench";
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

(很明显example.com被替换为真实域名)

var_dump($_SERVER)的部分输出;

array(34) {
["USER"]=>
string(8) "www-data"
["HOME"]=>
string(8) "/var/www"
["FCGI_ROLE"]=>
string(9) "RESPONDER"
["PHP_VALUE"]=>
string(26) "newrelic.appname=workbench"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_METHOD"]=>
string(3) "GET"
["CONTENT_TYPE"]=>
string(0) ""
["CONTENT_LENGTH"]=>
string(0) ""
["SCRIPT_FILENAME"]=>
string(39) "/website/workbench/api/public/index.php"
["SCRIPT_NAME"]=>
string(21) "/api/public/index.php"
["REQUEST_URI"]=>
string(15) "/api/projects/1"
["DOCUMENT_URI"]=>
string(21) "/api/public/index.php"
["DOCUMENT_ROOT"]=>
string(18) "/website/workbench"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_SOFTWARE"]=>
string(11) "nginx/1.4.6"
}

var_dump($_REQUEST) 给出一个空数组。

很明显,我的设置出现了严重错误,但我正在努力查看到底是什么!将 $query_string 更改为 $args 也没有任何效果。

最佳答案

这里的派对已经很晚了,但我在网站根目录中提供静态文件(一个 Angular 应用程序)和在/api 中提供一个 slim 应用程序时遇到了同样的问题。正如您所指出的,Slim 确实需要 REQUEST_URI在其中包含 /api

server {
server_name example.com;

location ~ ^/api/(.*)$ {
alias /path/to/slim-app/public/;
try_files $1 $1/ @php;
index index.php;
}

location / {
root /path/to/your/static/files;
}

location @php {
fastcgi_split_path_info ^(/api)(/.*)$;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/slim-app/public/index.php;
fastcgi_param REQUEST_URI $fastcgi_path_info;
fastcgi_read_timeout 900;
}
}

对我来说关键在于 fastcgi_split_path_info 技巧。根据 http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info ,该操作所做的是将 $fastcgi_path_info 变量拆分为两个 变量,分别称为 $fastcgi_script_name$fastcgi_path_info

当您输入 fastcgi_split_path_info ^(/api)(/.*)$; 时,您实际上将 $fastcgi_script_name 设置为 /api 并且将 $fastcgi_path_info 设置为 /my/route。我发现这足以让 Slim (v3) 以我想要的方式工作。

但是,我还发现我的默认 Slim 应用程序将 DOCUMENT_URLSCRIPT_NAME 变量设置为 index.php。所以你也可以设置它们(尽管它似乎不是必需的):

set $script_name "/index.php";
fastcgi_param DOCUMENT_URI $script_name;
fastcgi_param SCRIPT_NAME $script_name;

关于php - 如何在 nginx 的子目录中获取 Slim PHP Framework 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26015168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com