# Try\_files acting weird, maybe affected by IF?

**URL:** <https://www.ruby-forum.com/t/try-files-acting-weird-maybe-affected-by-if/220684>\
**Category:** NGINX\
**Created:** [June 16, 2012, 6:08pm UTC](https://www.ruby-forum.com/t/try-files-acting-weird-maybe-affected-by-if/220684 "2012-06-16T18:08:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 16, 2012, 6:08pm UTC](https://www.ruby-forum.com/t/try-files-acting-weird-maybe-affected-by-if/220684/1 "2012-06-16T18:08:48Z")

</div>

Hi,  
I’ve been trying to force nginx to dynamically handle hosts. This is my  
configuration file

```
    server {
            listen 127.0.0.1:80;
            server_name

```

~^(:?(?.+).)?(?[^.]+.[^.]+)$;  
index index.php index.html;

```
            location / {
                    set $try_dir $domain;
                    if (-d /var/www/hosts/$second.$domain) {
                            set $try_dir $second.$domain;
                    }

                    root /var/www/hosts/$try_dir;
                    try_files $uri $uri/ /index.php;
            }

            # pass the PHP scripts to FastCGI server listening on

```

127.0.0.1:9000  
location ~ .php$ {  
set $try\_dir $domain;  
if (-d /var/www/hosts/$second.$domain) {  
set $try\_dir $second.$domain;  
}

```
                    root /var/www/hosts/$try_dir;
                    include fastcgi_params;
                    fastcgi_param SERVER_NAME $try_dir;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;

                    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
                    fastcgi_param PATH_INFO $fastcgi_path_info;
                    fastcgi_param PATH_TRANSLATED

```

$document\_root$fastcgi\_path\_info;

```
                    fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;

```
                    try_files $uri =404;
            }
    }

```

It receives request, checks if exists corresponding subdirectory, if  
not, passes to “main” directory.  
I know I shouldn’t be using the IF statement, but I’ve tried many, but  
havent figured this out without the if.

So let’s have a request

```
    $ curl http://hosiplan.l/rewrite

```

The directory /var/www/hosts/hosiplan.l exists, there is index.php file  
and no “rewrite” file. This should be handled by the index.php

```
    ...
    [debug]: *1 using configuration "/"
    ...
    [debug]: *1 http script file op 0000000000000002

```

“/var/www/hosts/.hosiplan.l”  
[debug]: \*1 http script file op false  
[debug]: \*1 http script if  
[debug]: \*1 http script if: false  
…  
[debug]: \*1 try files phase: 11  
…  
[debug]: \*1 try to use file: “/rewrite”  
“/var/www/hosts/hohosiplan.l/rewrite”  
…  
[debug]: \*1 try to use dir: “/rewrite”  
“/var/www/hosts/hohosiplan.l/rewrite”  
…  
[debug]: \*1 try to use file: “/index.php”  
“/var/www/hosts/hohosiplan.l/index.php”  
[debug]: \*1 internal redirect: “/index.php?”  
…

This works like a charm. Pure perfection. But when I use a subdomain

```
    $ curl http://blaphp.hosiplan.l/rewrite

```

The directory /var/www/hosts/blaphp.hosiplan.l exists, there is  
index.php file and no “rewrite” file. This should be handled by the  
index.php

```
    ...
    [debug]: *3 using configuration "/"
    ...
    [debug]: *3 http script file op 0000000000000002

```

“/var/www/hosts/blaphp.hosiplan.l”  
[debug]: \*3 http script if  
…  
[debug]: \*3 try files phase: 11  
…  
[debug]: \*3 http filename:  
“/var/www/hosts/blaphp.hosiplan.l/rewrite”  
2012/06/16 17:05:45 [error] 2604#0: \*3 open()  
“/var/www/hosts/blaphp.hosiplan.l/rewrite” failed (2: No such file or  
directory), client: 127.0.0.1, server:  
~^(:?(?.+).)?(?[^.]+.[^.]+)$, request: “GET /rewrite  
HTTP/1.1”, host: “blaphp.hosiplan.l”

And that’s where I’m stuck. Any kind of input would be really  
appreciated.

Posted at Nginx Forum:

> **[try\_files acting weird, maybe affected by IF ?](https://forum.nginx.org/read.php?2,227602,227602#msg-227602)**
>
> Hi, I've been trying to force nginx to dynamically handle hosts. This is my configuration file server { listen 127.0.0.1:80; server\_name ~^(:?(?\<second\>.+)\\.)?(?\<domain\>\[^.\]+\\.\[^.\]+)$;

---

<div class="post-metadata">

**Author:** ![HosipLan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HosipLan](https://www.ruby-forum.com/u/HosipLan)\
**Post date:** [June 16, 2012, 11:58pm UTC](https://www.ruby-forum.com/t/try-files-acting-weird-maybe-affected-by-if/220684/2 "2012-06-16T23:58:10Z")

</div>

Hello!

On Sat, Jun 16, 2012 at 12:08:35PM -0400, HosipLan wrote:

> ```
> location / {
> 
> ```
> 
> 127.0.0.1:9000  
> fastcgi\_index index.php;  
> }  
> $ curl [http://hosiplan.l/rewrite](http://hosiplan.l/rewrite)  
> [debug]: \*1 http script if  
> [debug]: \*1 try to use file: “/index.php”  
> index.php  
> [debug]: \*3 http filename:  
> “/var/www/hosts/blaphp.hosiplan.l/rewrite”  
> 2012/06/16 17:05:45 [error] 2604#0: \*3 open()  
> “/var/www/hosts/blaphp.hosiplan.l/rewrite” failed (2: No such file or  
> directory), client: 127.0.0.1, server:  
> ~^(:?(?.+).)?(?[^.]+.[^.]+)$, request: “GET /rewrite  
> HTTP/1.1”, host: “blaphp.hosiplan.l”
> 
> And that’s where I’m stuck. Any kind of input would be really  
> appreciated.

In the second case the “if (-d …)” is matched, and you are  
hitting “try\_files won’t work due to if” example from here:

> **[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

Trivial solution in this case is to move your ifs to server  
level.

Maxim D.

---

<div class="post-metadata">

**Author:** ![HosipLan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HosipLan](https://www.ruby-forum.com/u/HosipLan)\
**Post date:** [June 17, 2012, 10:47am UTC](https://www.ruby-forum.com/t/try-files-acting-weird-maybe-affected-by-if/220684/3 "2012-06-17T10:47:16Z")

</div>

You’ve saved my day! Thank you wery much!

Posted at Nginx Forum:

> **[Re: try\_files acting weird, maybe affected by IF ?](https://forum.nginx.org/read.php?2,227602,227618#msg-227618)**
>
> Hi, I've been trying to force nginx to dynamically handle hosts. This is my configuration file server { listen 127.0.0.1:80; server\_name ~^(:?(?\<second\>.+)\\.)?(?\<domain\>\[^.\]+\\.\[^.\]+)$;
