【问题标题】:Using regex matches from nginx location in try_files directive在 try_files 指令中使用来自 nginx 位置的正则表达式匹配
【发布时间】:2016-03-17 16:02:18
【问题描述】:

我在 nginx 服务器块配置中有以下位置块:

location /eci-new/public {
    try_files  $uri   $uri/ /eci-new/public/index.php?$query_string;
}

location /eci-integration/public {
    try_files  $uri   $uri/ /eci-integration/public/index.php?$query_string;
}

它可以工作,但我将来需要添加更多位置,我想将其减少到只有一个使用 uri 段来应用 try_files 指令配置的位置块,如下所示:

location ~ /(.*)/public {
    try_files  $uri   $uri/ /$1/public/index.php?$query_string;
}

但是当我尝试加载页面时,我在 chrome 的控制台中收到了这条消息。

资源被解释为 Document,但使用 MIME 类型 application/octet-stream: "http://33.33.33.33/eci-new/public/rms/product-normalization" 传输。

如何使 nginx 配置正常工作?并将请求发送到 php 处理器?

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    因为您已将前缀位置更改为正则表达式位置,它现在与您的 php 位置冲突。所以/eci-new/public/index.php URI 不会被解释为 php 脚本。

    移动 php 位置(类似于location ~* \.php),使其高于您的新正则表达式位置,因此优先用于 php 脚本。

    另外,您可能希望收紧您的正则表达式并在开头添加 ^

    location ~ ^/(.*)/public { ... }
    

    详情请参阅this document

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 2014-11-11
      • 2021-09-05
      • 1970-01-01
      • 2021-03-19
      • 2017-10-26
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多