[英]How to get Slim PHP Framework routes in a subdirectory on nginx
我正在嘗試將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
變量並查看它們。 但是,路由沒有生效,請求變量似乎是空的。
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)
部分輸出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
更改$query_string
$args
也沒有任何效果。
這里聚會很晚,但是我在網站的根目錄中提供靜態文件(一個Angular應用程序)和在/ api中提供一個苗條的應用程序時遇到了同樣的問題。 正如您所指出的,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
設置$fastcgi_script_name
/api
並將$fastcgi_path_info
設置$fastcgi_path_info
/my/route
。 我發現這足以讓Slim(v3)以我想要的方式工作。
但是,我還發現我的默認Slim應用程序將DOCUMENT_URL
和SCRIPT_NAME
變量設置為index.php
。 所以你也可以設置它們(盡管似乎不需要):
set $script_name "/index.php";
fastcgi_param DOCUMENT_URI $script_name;
fastcgi_param SCRIPT_NAME $script_name;
我想你可以從try_files
刪除$query_string
try_files
。 在/api
位置添加root
指令,如下所示:
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
文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.