Nginx 位置和嵌套的 Slim 索引

Nginx locations and nested Slim indexes

使用 php Slim 框架并具有以下结构:

/root
    /dir_parent_1/
        /dir_child_1
            /index.php
    /dir_parent_2
        /index.php

/root/dir_parent_1/dir_child_1/index.phpusers 路线,并且 /root/dir_parent_2/index.phpclients 路线。

我如何编写 Nginx 位置以匹配两种可能的组合?

我试试:

location ~ ^/root/(\w+)
{
    try_files $uri /gestionhospitales/api//index.php?$query_string;
}

location ~ ^/root/(\w+)/(\w+)
{
    try_files $uri /gestionhospitales/api///index.php?$query_string;
}

但它们是相互排斥的。去“/root/dir_parent_1/dir_child_1/users”匹配第一个规则(因为它是第一个。如果我颠倒顺序,第二个匹配),

我也开始为这两种情况编写规则,但我不知道如何完成:

location ~ ^/root/(\w+)?/(\w*)
{
    try_files $uri /gestionhospitales/api///index.php?$query_string;
#  tried with if, rewrite, etc. But i'm not nginx expert.
}

提前致谢。

您必须设置的唯一路径是将所有内容转发到 public 目录中的 index.php 文件。

root         /path/www.mysite.com/public_html;

try_files $uri /index.php;

此文件应包含 php 用于增强 Slim 框架的代码。然后所有 "routes" 将根据您在 index.php 中的代码由 PHP 和 Slim 处理,而不是由 nginx 处理。

$app->get('/users', function () {
    echo "This is the user route!";
});

$app->get('/clients', function () {
    echo "This is the client route!";
});

你可以在官方文档中看到一个完整的例子here