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

使用NGINX将所有子位置路由到index.php,php,nginx,fastcgi,Php,Nginx,Fastcgi,我想在我的php脚本中解析URL,因此我需要从特定位置到index.php的所有内容 但是,当直接导航到该文件时,我只能看到特定的php文件 例如,请求 mycomain.ext/api mycomain.ext/api/user mycomain.ext/api/user/19 mycomain.ext/api/user/19/detail 应该全部路由到api/index.php 当前配置: location /api { root /usr/share/nginx/php/;

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

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


例如,请求

  • mycomain.ext/api
  • mycomain.ext/api/user
  • mycomain.ext/api/user/19
  • mycomain.ext/api/user/19/detail
  • 应该全部路由到
    api/index.php


    当前配置:

     location /api {
        root /usr/share/nginx/php/;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
      }
    

    是的,因为它指向
    /index.php
    ,而不是预期的
    /api/index.php
    。只需将try_文件更改为
    try_文件/api/index.php?$args=404。并且有一个专用的
    /api/index.php
    位置,您可以将请求转发到php fpm


    本机没有
    $query\u string
    变量,只有
    $args
    变量可以传递所有参数。只有在正确编码的情况下,才应使用变量
    $request\u uri
    。您的php脚本应该使用
    explode('?',$\u SERVER['REQUEST\u URI',2)[0]
    来处理正确的请求