# Redirect loop with try\_files

**URL:** <https://www.ruby-forum.com/t/redirect-loop-with-try-files/228709>\
**Category:** NGINX\
**Created:** [May 4, 2013, 4:21pm UTC](https://www.ruby-forum.com/t/redirect-loop-with-try-files/228709 "2013-05-04T16:21:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [May 4, 2013, 4:21pm UTC](https://www.ruby-forum.com/t/redirect-loop-with-try-files/228709/1 "2013-05-04T16:21:03Z")

</div>

Hi,

I have a front end nginx webserver with behind it a tomcat server. I ran  
into a nasty little redirect loop issue that I still don’t understand.  
I’m  
trying to redirect users who have a session cookie to a different page  
than  
the other users

I have a configuration such as this:

## Rewrite index requests

rewrite ^(._)/index.(._)$ $1/ permanent;

map $cookie\_msa\_country $ctry {  
default 0;  
NL “nederland/NL\_”;  
BR “brasil/BR\_”;  
}

map $cookie\_msa\_lng $lng {  
default “nl”;  
nl “nl”;  
pt “pt”;  
es “es”;  
en “en”;  
}  
location = / {  
set $red 0;  
if ($http\_cookie ~\* “JSESSIONID”){  
set $red 1;  
}  
if ($ctry = 0){  
set $red 0;  
}  
if ($red = 1){  
rewrite ^(.\*)$ http://$host/$ctry$lng/account/wall/;  
}

```
 try_files /notthere.html @proxy;

```

}

When a users types in domain [www.site.com](http://www.site.com), he ends up in an endless  
loop. I  
tried various things but without luck. Any suggestions how to avoid  
this?

Posted at Nginx Forum:

> **[redirect loop with try\_files](https://forum.nginx.org/read.php?2,238837,238837#msg-238837)**
>
> Hi, I have a front end nginx webserver with behind it a tomcat server. I ran into a nasty little redirect loop issue that I still don't understand. I'm trying to redirect users who have a session cookie to a different page than the other users I have...

---

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [May 4, 2013, 10:56pm UTC](https://www.ruby-forum.com/t/redirect-loop-with-try-files/228709/2 "2013-05-04T22:56:33Z")

</div>

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:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

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

> When a users types in domain [www.site.com](http://www.site.com), 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 [http://www.site.com/](http://www.site.com/)”, 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.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
