使用php Slim框架并具有以下结构:
/root
/dir_parent_1/
/dir_child_1
/index.php
/dir_parent_2
/index.php
/root/dir_parent_1/dir_child_1/index.php
有users
路由,且/root/dir_parent_2/index.php
有clients
路由
我如何写Nginx的位置来匹配两种可能的组合?
我试一试:
location ~ ^/root/('w+)
{
try_files $uri /gestionhospitales/api/$1/index.php?$query_string;
}
location ~ ^/root/('w+)/('w+)
{
try_files $uri /gestionhospitales/api/$1/$2/index.php?$query_string;
}
但它们是相互排斥的。进入"/root/dir_parent_1/dir_child_1/users"匹配第一条规则(因为它是第一条)。如果顺序颠倒,则匹配第二个),
我也开始为这两种情况写一个规则,但我不知道如何完成它:
location ~ ^/root/('w+)?/('w*)
{
try_files $uri /gestionhospitales/api/$1/$2/index.php?$query_string;
# tried with if, rewrite, etc. But i'm not nginx expert.
}
您必须设置的唯一路由是将所有内容转发到公共目录中的index.php文件。
root /path/www.mysite.com/public_html;
try_files $uri /index.php;
这个文件应该包含启动Slim框架的php代码。然后所有的"路由"将由PHP和Slim根据你在index.php中的代码来处理,而不是由nginx。
$app->get('/users', function () {
echo "This is the user route!";
});
$app->get('/clients', function () {
echo "This is the client route!";
});
您可以在官方文档中看到完整的示例。