我正在尝试将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;
}
}
Run Code Online (Sandbox Code Playgroud)
(显然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"
}
Run Code Online (Sandbox Code Playgroud)
var_dump($_REQUEST)
给出一个空数组.
很显然,我的设置出现了严重错误,但我很难看到什么!更改$query_string
到$args
也无济于事.
小智 10
这里聚会很晚,但是我在网站的根目录中提供静态文件(一个Angular应用程序)并在/ api中提供一个苗条的应用程序时遇到了同样的问题.正如你指出的那样,斯利姆真正需要的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;
}
}
Run Code Online (Sandbox Code Playgroud)
对我而言,关键在于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)以我想要的方式工作.
不过,我也发现,我的默认苗条的应用程序有DOCUMENT_URL
和SCRIPT_NAME
变量设置为index.php
.所以你也可以设置它们(尽管似乎不需要):
set $script_name "/index.php";
fastcgi_param DOCUMENT_URI $script_name;
fastcgi_param SCRIPT_NAME $script_name;
Run Code Online (Sandbox Code Playgroud)
我认为你可以$query_string
从 中删除try_files
。root
在位置添加指令/api
,如下所示:
location /api {
root /website/workbench/api;
try_files $uri $uri/ /public/index.php;
}
Run Code Online (Sandbox Code Playgroud)
并使用fastcgi_param SCRIPT_FILENAME
最终定义为的参数:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Run Code Online (Sandbox Code Playgroud)
到fastcgi_params
文件中。
归档时间: |
|
查看次数: |
4721 次 |
最近记录: |