redirect loop with try_files

Francis Daly francis at
Sat May 4 20:55:28 UTC 2013

On Sat, May 04, 2013 at 10:20:32AM -0400, mschipperheyn wrote:

Hi there,

> ## Rewrite index requests
> rewrite ^(.*)/index.(.*)$ $1/ permanent;

That is likely to lead to a loop, unless you take great care elsewhere.

(The typical defaults are: a request for /dir/ leads to an internal
rewrite to /dir/index.html, which the above would convert to an external
redirect to /dir/, which is where we came in.)

> location = / {
> 	   set $red  0;
> 	   if ($http_cookie ~* "JSESSIONID"){
> 			   set $red 1;
> 	   }

Here, you use "if" inside "location" and you do something other than
"return" or "rewrite ... last". That's rarely a good idea:

Can you move the if/set logic to server{} level, to avoid that likely

> When a users types in domain, he ends up in an endless loop.

I strongly suspect that the loop is caused by the first rewrite, because
the try_files does not apply because at least one of your "if" conditions
is true.

You can test that by trying "curl -i", and adding
whatever is necessary to make sure that none of the "if" conditions match,
and seeing if you get anything different.

Or you can just redo the configuration to avoid most "if" inside
"location" blocks.

Francis Daly        francis at

More information about the nginx mailing list