【发布时间】:2016-05-26 08:04:31
【问题描述】:
我正在尝试将 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 也无效。
【问题讨论】: