Nginx 重寫和返回幹擾不同的位置區塊

Nginx 重寫和返回幹擾不同的位置區塊

我在 nginx 中有以下配置,用於在特定場景下進行重定向。

location /prefix-someurl {
   if (condition) {
            return 301 $scheme://$host/xyz.html;
   }
   proxy_pass someValue;
}

在另一個區塊中有一些像這樣的重寫規則

location /someurl {
   if (condition) {
            rewrite ^(.*)abc(.*)$ $1test/abc$2 break;               
            rewrite ^(.*)someurl/$(.*) $1someurl/test/index.html$2;

   }
   proxy_pass value;
}

上述配置按預期工作。但是,如果對於後者我進行此修改;

location /someurl {
   if (condition) {
            return 301 $scheme://$host/xyz.html;
   }
   if (condition) {
            rewrite ^(.*)abc(.*)$ $1test/abc$2 break;               
            rewrite ^(.*)someurl/$(.*) $1someurl/test/index.html$2;

   }
   proxy_pass value;
}

我訪問時遇到太多重定向錯誤/prefix-someurl.如果我將其修改/someurl為以下內容,錯誤就會消失;

location /someurl {
  if (condition) {
        return 301 $scheme://$host/xyz.html;
  }    
  proxy_pass value;
}

我不明白為什麼修改/someurl會對/prefix-someurl.或者我是否遺漏了有關 nginx 重寫評估的內容。對此的見解將不勝感激。

相關內容