Try_files and location blocks

Dave Hayes dave at jetcafe.org
Fri Jun 24 22:14:57 MSD 2011


appa at perusio.net writes:
> Also your later config could be simplified:
>   server {
>      ...
>      location / {
>         try_files $uri $uri/ /error.php?c=404 =404;
>      }	  
>   }

In this case, it seems to me that try files is looking for a file
named "/error.php?c=404" and not "/error.php". Try_files does not
appear to strip the arguments from the uri when trying. My evidence
for this is this debug line:

2011/06/24 03:11:58 [debug] 20773#0: *1 try to use file: "/error.php?c=403?" 
"/home/www/testhome/error.php?c=403?"

This means that I can't see if error.php exists before I try to throw an
error with it. Right now it seems that my solution is:

  location / {
     try_files $uri $uri/ =404;
  }
     
  location ~ .php$ {
     <..fastcgi stuff..>
  }

  error_page 404 = /error.php?c=404;

  location = /error.php {
     try_files /error.php @low_level_404
     <..the -same- fastcgi stuff as above..>
  }

  location @low_level_404 {
      return 404;
  }

I am really trying hard not to use "if" but I am weakening here because
an "if" would let me use one and only one instance of fastcgi:

 location / {
     try_files $uri $uri/ =404;
  }
     
  location ~ .php$ {
     <..fastcgi stuff..>
  }

  error_page 404 = @error_404
  location @error_404 {
     if (! -f $document_root/error.php) {
        return 404;
     }
     rewrite ^ $scheme://$server_name/error.php?c=404
  }

I'm also curious (in an academic way) as to what use case exists when
try_files has to match url arguments as a filename? I usually strongly
discourage the use of '?' and '&' characters in a filename. 
-- 
Dave Hayes - Consultant - Altadena CA, USA - dave at jetcafe.org 
>>> The opinions expressed above are entirely my own <<<

Treat people as if they are what they ought to be, and you
help them to become what they are capable of being.











More information about the nginx mailing list