Try_files and location blocks
António P. P. Almeida
appa at perusio.net
Fri Jun 24 22:48:44 MSD 2011
On 24 Jun 2011 19h14 WEST, dave at jetcafe.org wrote:
> 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?"
No. It doesn't. Check the code. It tries a named location otherwise
invokes ngx_http_split_args(). That's just a concise way of writing a
debug line in the error log.
> 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:
Yes you can.
> 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..>
> }
This doesn't make sense. This is an exact location. It's entered from
a try_files, a rewrite or when you request that file directly. If the
file doesn't exist it signals a 404.
> 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..>
> }
>
No need for an if. At least that's what I conclude from the previous
discussion. This should be all you need for the situation you
presented.
location / {
try_files $uri $uri/ /error.php?c=404 =404;
}
location = /error.php {
(...) # fastcgi stuff
}
> 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.
You're taking the peculiar way the debug line is written as the real
way things are done.
Here's the relevant code from the http_core_module.c source (starting
at line 1241):
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"try to use %s: \"%s\" \"%s\"",
test_dir ? "dir" : "file", name, path.data);
--- appa
More information about the nginx
mailing list