1

I have the following configuration in nginx for redirecting in a certain scenario.

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

and in another block there are some rewrite rules like this

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

   }
   proxy_pass value;
}

The above configuration works as intended. However if for the latter I make this modification;

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;
}

I get too many redirect errors for accessing /prefix-someurl.If I modify the /someurl to the following the error goes away;

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

I cannot understand why modifying /someurl has an impact on /prefix-someurl. Or am I missing something regarding nginx rewrite evaluation. Insight on this would be appreciated.

Syed Osama Maruf
  • 103
  • 2
  • 2
  • 9
  • 1
    You need to identify each step in the loop. Use `curl -I` to test the individual redirects. Also, enable the [`rewrite_log`](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite_log) to get diagnostics from the rewrite engine. – Richard Smith Jul 28 '21 at 08:21
  • You can most likely find an answer at https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ . Instead of `if`, you should use `map` and use another strategy for your conditions. – Tero Kilkanen Jul 28 '21 at 11:37

0 Answers0