首页 > 解决方案 > 使用 NGINX 将所有子位置路由到 index.php

问题描述

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

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


例如,请求;

  1. mycomin.ext/api
  2. mycomin.ext/api/user
  3. mycomin.ext/api/user/19
  4. mycomin.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;
  }

标签: phpnginxfastcgi

解决方案


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


Native 没有$query_string变量,只有$args传递所有参数的变量。$request_uri只有在正确编码的情况下,才不应使用该变量。您的 php 脚本应该explode('?', $_SERVER['REQUEST_URI'], 2)[0]用来处理正确的请求。


推荐阅读