我正在尝试将Slim应用程序移至子目录,以便可以在处对其进行访问example.com/api/
,但是在使路由正常工作时遇到了严重的问题。
主要脚本位于/website/workbench/api/public/index.php
,因此对的调用example.com/api/project/1
应到达API文件夹。但是,我还需要能够访问example.com的index.html
文件(该文件在Angular JS上运行)。
当我转到时,它确实命中了PHP脚本example.com/api/project/1
-我可以var_dump
查看变量。但是,路由未生效,并且请求变量似乎为空。
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
也无效。
我想你可以删除$query_string
的try_files
。root
在/api
位置添加指令,如下所示:
location /api {
root /website/workbench/api;
try_files $uri $uri/ /public/index.php;
}
并使用fastcgi_param SCRIPT_FILENAME
最终定义为的参数:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
到fastcgi_params
文件中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句