php - 使用 NGINX 将所有子位置路由到 index.php
问题描述
我想在我的 php 脚本中解析 URL,因此我需要从特定位置到路由到 index.php 的所有内容。
但是,直接导航到文件时,我只能看到特定的 php 文件。
例如,请求;
- mycomin.ext/api
- mycomin.ext/api/user
- mycomin.ext/api/user/19
- 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;
}
解决方案
是的,因为它指向/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]
用来处理正确的请求。
推荐阅读
- python - 是否可以在 python Altair Heatmap 图中添加黑色网格线?
- html - Angular 中的 Corona 应用程序 - 根据表格值动态字/表格换行 - 无法在 A4 内生成
- python - 如何在 Python 中按分组列查找 ngram
- keystonejs - 如何在 keystonejs 5 中为默认应用程序激活 CORS?
- prolog - Prolog 中的标签变量
- c# - 如何将具有差异内容的 2 个文件合并为 json 对象
- ruby-on-rails - 有没有办法从 Sunspot Solr 查询中获取至少 N 条记录?
- c - 指针 - 在两个文件中读取、写入、复制 c 中的文本
- c# - 静态 HttpClient 但仍然有太多打开的套接字
- listview - Listview.builder的builditem函数中的Flutter Setstate未重新加载