【发布时间】:2016-07-23 01:45:36
【问题描述】:
我试图弄清楚为什么前面的 / 不允许 \.php$ 位置指令拾取脚本。
下面是我用于 nginx 的示例 .conf。我们正在运行 API 代码,版本化路径为://domian.com/v#.#/endpoint/uri
我们的document_root是版本号的下一个目录; index.php 存在于版本目录中:path/to/sites/public/v1.0/index.php
index index.php # <-- this is in here globally
location ~ ^/(?<version>v[\d\.]+) {
try_files $uri $version/index.php?$args;
# Why does this NOT work? The / stopping \.php$ from matching
# try_files $uri /$version/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass php56;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
[...]
}
我尝试了各种方法,但似乎无法正常工作。一旦我删除了/,它就会按预期工作,但是我的 SCRIPT_FILENAME 行将从:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
收件人:
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
这会破坏直接的 PHP 文件调用——这是我不想要的。
编辑:解决方案:回答如下:问题与匹配顺序有关。模式匹配是最先发现的,而不是最后发现的。
切换他们的订单有效。第一个 try_files 失败了,因为该模式明确寻找以 / 开头。
我正在阅读:http://nginx.org/en/docs/http/request_processing.html
请求“/index.php”也匹配前缀位置“/” 首先是正则表达式“.(php)$”。因此,它由后一个位置处理,并将请求传递给在 localhost:9000 上侦听的 FastCGI 服务器。
【问题讨论】:
-
您在第一个
location部分尝试过index index.php吗? -
或者,在你的
http部分会更好。 -
不起作用。我确实有一个全局
index指令。我在这里加了一个,也不行。 Nginx 将文件/v1.0/index.php提取为静态文件并直接显示(我在全局范围内添加了default_type text/plain以防止应用程序/oct...流的下载默认值)。 -
没有任何
try_files指令的行为是什么?我发现index单独为我工作,但我几周前才开始使用 Nginx,经过多年的 Apache 工作,所以我很可能不知道我在说什么!