# Try\_files and redirect loops

**URL:** <https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918>\
**Category:** NGINX\
**Created:** [April 18, 2011, 5:37pm UTC](https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918 "2011-04-18T17:37:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 18, 2011, 5:37pm UTC](https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918/1 "2011-04-18T17:37:52Z")

</div>

Hi all!

I need to make sure try\_files does not send to a static file if $args  
are included.

```
 #if ($args != "") {rewrite ^ @ app;} # tried here and below.

 location = / { rewrite ^ /home/index/$host; }
 location / {
   if ($args = "") { rewrite ^ @app;}
   try_files /downtime.html $uri/index.html $uri @app;# double

```

rewrite error  
}

```
 location @app {
   proxy_pass http://app_server;
   ...

```

I also tried something like this, but “break” makes it not match the  
location @app either.

Thanks for any help you can give me, my forehead is sore from pounding  
it against the keyboard 🙂  
-Kevin

```
 location / {
   # reload=auto refreshes cached content. hmm, easy to have this

```

match twice which causes problems!  
if ($http\_cache\_control ~\* “no-cache”) { rewrite ^ $uri?recache;  
}  
if ($args != “”) {rewrite ^ @app;}

```
   location = / { rewrite ^ /home/index/$host; }
   location ~* /$ { rewrite ^ $uri/index.html; }

   # after break won't match any location rules either :(
   if (!-f $uri) {rewrite ^ @app; break;}
 }

 location @app {
   proxy_pass http://app_server;

```

Posted at Nginx Forum:

> **[try\_files and redirect loops](https://forum.nginx.org/read.php?2,192095,192095#msg-192095)**
>
> Hi all! I need to make sure try\_files does not send to a static file if $args are included. #if ($args != "") {rewrite ^ @ app;} # tried here and below. location = / { rewrite ^ /home/index/$host; } location / {

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [April 18, 2011, 6:31pm UTC](https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918/2 "2011-04-18T18:31:39Z")

</div>

On Mon, Apr 18, 2011 at 11:37:06AM -0400, inspire22 wrote:

Hi there,

> I need to make sure try\_files does not send to a static file if $args  
> are included.

So… what do you want to happen if $args are included?

I’ll guess “check for /downtime.html, then try the app server”. In which  
case, the following might be close:

===  
server {  
set $u1 $uri/index.html;  
set $u2 $uri;  
if ($is\_args) {  
set $u1 “”;  
set $u2 “”;  
}

try\_files /downtime.html $u1 $u2 @app;

# location @app { proxy\_pass http://app\_server; } }

You’ll need some extra bits, for example to define app\_server, but it  
might give some inspiration.

Good luck with it,

## f

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

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [April 18, 2011, 7:50pm UTC](https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918/3 "2011-04-18T19:50:49Z")

</div>

Clever, thanks! Working pretty well 🙂 Is there any way to print a  
variable to the error log so I can debug some more-complicated logic  
with setting things?

How on earth can try\_files be more efficient than a specific if-list?  
Doesn’t it have to stat more files?

Posted at Nginx Forum:

> **[Re: try\_files and redirect loops](https://forum.nginx.org/read.php?2,192095,192133#msg-192133)**
>
> Hi all! I need to make sure try\_files does not send to a static file if $args are included. #if ($args != "") {rewrite ^ @ app;} # tried here and below. location = / { rewrite ^ /home/index/$host; } location / {

---

<div class="post-metadata">

**Author:** ![inspire22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a4c791/32.png) [@inspire22](https://www.ruby-forum.com/u/inspire22)\
**Post date:** [April 18, 2011, 8:46pm UTC](https://www.ruby-forum.com/t/try-files-and-redirect-loops/205918/4 "2011-04-18T20:46:24Z")

</div>

On Mon, Apr 18, 2011 at 01:50:27PM -0400, inspire22 wrote:

Hi there,

> Clever, thanks! Working pretty well 🙂

Good stuff.

> Is there any way to print a  
> variable to the error log so I can debug some more-complicated logic  
> with setting things?

I’ve usually been able to find what I needed in the debug log.

If you’re going to try setting and logic in the config file, you’ll want  
to make sure that you understand how things are processed. Especially  
“if” inside “location”.

Check the wiki and mail archives for more.

Good luck with it,

## f

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