我已经在 stackoverflow 上查看了建议的解决方案,但没有找到任何解决我问题的方法:
- Nginx + phpFPM:PATH_INFO 始终为空
- $_SERVER 中的 PATH_INFO 始终为空 - NGINX + FPM 7.3 + Ubuntu 18.04
- 使用 PATH_INFO 重写 Nginx 配置
- 别名环境中的 nginx path_info
- nginx 中 PATH_INFO 的空值返回垃圾值
- Nginx:隐藏 .php 扩展名但使用 PATH_INFO
- Debian 9 + PHP7.0-FPM + NGINX 1.10.3-1 path_info 问题
- 在 nginx 上运行的 CodeIgniter 应用程序上发布数组始终为空
问题是 PATH_INFO 仍然是空的
请求可以这样:route-php.local/show
$_SERVER['REQUEST_URI']
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string '' (length=0)
....
/home/niko/web/www/route-php.local/public/index.php:51:
array (size=1)
'_route' => string 'show' (length=4)
但这个请求不好:route-php.local/show?p=test
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string 'p=test' (length=6)
....
路径无法识别,因为它是空的......?为什么 ?
$_SERVER['PATH_INFO']
与:route-php.local/show
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string '' (length=0)
....
/home/niko/web/www/route-php.local/public/index.php:51:
array (size=1)
'_route' => string 'index' (length=4)
这是我的设置:
PHP-7.4 和 composer 我安装了 symfony/routing
https://packagist.org/packages/symfony/routing
/etc/php/7.3/fpm/php.ini
/etc/php/7.3/cli/php.ini
/etc/php/7.3/apache2/php.ini
cgi.fix_pathinfo=1 # And I tried cgi.fix_pathinfo=0
/etc/nginx/sites-available/route-php.local
location / {
try_files $uri /index.php;
}
location ~ [^/]\.php(/|$) {
root /var/www/localhost;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
include snippets/fastcgi-php.conf;
}
snippets
/etc/nginx/snippets/fastcgi-php.conf
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $path_info; # or fastcgi_param PATH_INFO $fastcgi_path_info
fastcgi_index index.php;
include fastcgi.conf;
我尝试了几种解决方案,但没有任何效果....
示例配置 Symfony:https ://symfony.com/doc/current/setup/web_server_configuration.html#nginx
我的代码PHP:
## index.php ##
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
require __DIR__ . '/vendor/autoload.php';
$listeRoute = new Route("/");
$createRoute = new Route("/create");
$showRoute = new Route("/show");
// COLLECTION ROUTES
$collection = new RouteCollection();
$collection->add("list", $listeRoute);
$collection->add("create", $createRoute);
$collection->add("show", $showRoute);
//MATCHER
$matcher = new UrlMatcher($collection, new RequestContext());
var_dump($_SERVER);
$pathInfo = $_SERVER['PATH_INFO'] ?? '/';
$resultat = $matcher->match($pathInfo);
var_dump($resultat);
die();
另一个例子:
php -
|-index.php
|-text.php
#index.php
var_dump($_SERVER['PATH_INFO']);
echo "index.php";
#test.php
var_dump($_SERVER['PATH_INFO']);
echo "test.php";
cgi.fix_pathinfo=1
location / {try_files $uri $uri/ /$uri.php$is_args$args;}
location ~ \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.3-fpm.sock
}
#localhost/index
string(0) ""
index.php
#localhost/test
string(0) ""
test.php
为什么它会这样?
我想要这种行为
<?php
echo "PATH INFO => ";
var_dump($_SERVER['PATH_INFO']);
echo "<br>";
echo "REQUEST_URI => ";
var_dump($_SERVER['REQUEST_URI']);
// redefine the variable PATH_INFO
$_SERVER['PATH_INFO'] = explode('?', $_SERVER['REQUEST_URI']);
echo "<br>";
echo "NEW - PATH INFO => ";
var_dump($_SERVER['PATH_INFO'][0]);
echo "<br>";
echo "index.php";
# https://localhost/index?test=test
PATH INFO => string(0) ""
REQUEST_URI => string(16) "/index?test=test"
NEW - PATH INFO => string(6) "/index"
index.php