Nginx没有重定向到上游位置

问题描述

我以为我已经了解了Nginx重写规则的基础。我错了。

您能告诉我我在做什么错吗?

    server {
    listen 80;
    server_name myserver;
    index index.php index.html index.htm;
    root /apps/user/websites/;

    location / {
     autoindex on;
    }
   location ~ myapp {
       try_files $uri $uri /office/myapp/api/public/index.php;
   }
   location ~\.php$ {
      try_files $uri $uri /office/fake/index.html;
#      include fastcgi.conf;
#      fastcgi_pass php-fpm-upstream;
    }
}

如果我访问http:// myserver / office,将访问index.php,但不会像我想的那样被最后一个位置(〜.php $)捕获,而是将其作为文本文件处理并发送浏览器。 我原本希望收到office / fake / index.html文件。

非常感谢你, 安德烈斯

解决方法

正则表达式匹配位置的检查顺序与在配置文件中出现的顺序相同。拥有

location ~ myapp {
    try_files $uri $uri /office/myapp/api/public/index.php;
}

之前

location ~\.php$ {
    try_files $uri $uri /office/fake/index.html;
#    include fastcgi.conf;
#    fastcgi_pass php-fpm-upstream;
}

将为每个包含myapp子字符串的URI(除了那些与现有文件匹配且不以.php结尾的URI都提供一个无限循环),因为/office/myapp/api/public/index.php URI与{{1 }}正则表达式。您应该交换这些位置,以使此配置可行。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...