try_files acting weird, maybe affected by IF ?

Maxim Dounin mdounin at mdounin.ru
Sat Jun 16 21:57:26 UTC 2012


Hello!

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

> 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>[^.]+\.[^.]+)$;
>                 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:
> ~^(:?(?<second>.+)\.)?(?<domain>[^.]+\.[^.]+)$, 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:

http://wiki.nginx.org/IfIsEvil#Examples

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

Maxim Dounin



More information about the nginx mailing list