使用NGINX将所有子位置路由到index.php

时间:2020-03-09 10:13:23

标签: php nginx fastcgi

我想在我的php脚本中解析URL,因此,我需要从特定位置到路由到index.php的所有内容。

但是,当我直接导航到特定的php文件时,只能看到该文件。


示例,请求;

  1. mycomain.ext / api
  2. mycomain.ext / api / user
  3. mycomain.ext / api / user / 19
  4. mycomain.ext / api / user / 19 / detail

所有路线都应到达io_lib:format


当前配置:

api/index.php

1 个答案:

答案 0 :(得分:1)

是,因为它按预期指向/index.php而不是/api/index.php。只需将try_files更改为try_files /api/index.php?$args =404;。并在专用的/api/index.php位置将请求转发到php-fpm。


本机没有$query_string变量,只有$args变量可以传递所有参数。仅当变量$request_uri正确编码后才可以使用。您的php脚本应使用explode('?', $_SERVER['REQUEST_URI'], 2)[0]处理正确的请求。