【问题标题】:Nginx location path matchingNginx 位置路径匹配
【发布时间】:2017-02-14 22:46:04
【问题描述】:

很简单,但由于某种原因,我想不出正确的语法:

location /ios/ {
     try_files $uri $uri/ /ios/index.php$is_args$args;
}

location /cap/ {
     try_files $uri $uri/ /cap/index.php$is_args$args;
}

location /aww/ {
     try_files $uri $uri/ /aww/index.php$is_args$args;
}

我想将所有这些结合起来,并提供在顶级目录中添加更多应用程序的选项。

设置是:每个目录都是它自己的应用程序(code-igniter 或 laravel)并驻留在 domain.com/:app

我正在尝试在这里合并路径(产品站点是 Apache,在这里使用 nginx),但是我所有的尝试都失败了。

我尝试了~ /([\w\-]+)/ [...] /$1/index.php[...],但是失败了。仅使用 ~ /ios/ 失败(导致页面下载)。

我在这里错过了什么?

【问题讨论】:

  • 为什么会失败?会发生什么?
  • @FaisalMemon 页面下载。或404。主要是页面下载。很奇怪,就像 PHP 没有被解释一样,即使我有标准的 \.php$ 位置捕捉器。

标签: nginx configuration


【解决方案1】:

您可以使用命名位置对正确的index.php 文件执行内部重写:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(/[^/]+)/ $1/index.php$is_args$args last;
    return 404;
}

请参阅this document 了解更多信息。

【讨论】:

  • 完美!这似乎按预期工作,包括 php 解释。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-11-14
  • 2017-04-25
  • 2011-07-11
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多