Try_files and location blocks

Dave Hayes dave at jetcafe.org
Fri Jun 24 01:37:11 MSD 2011


Hello. :) Could someone please correct my understanding of location
blocks and try files? I'm trying to get http errors to be very graceful,
normally being handled by php but in extreme cases returning the proper
numeric error. For my example, just consider 404:

  server {
     ...
     try_files $uri $uri/ /error.php?c=404
     
     error_page 404 = @error_404;
     location @error_404 {
         try_files /error.php @not_found;
         rewrite ^ $scheme://$server_name/error.php?c=404
     }
     location @not_found {
         return 404;
     }
  }

I'm not clear why this does not work. I rewrite the above example
as:

  server {
     ...
     location / {
        try_files $uri $uri/ /error.php?c=404;
     }	  
     
     location /error.php {
        try_files /error.php @not_found;
     }

     error_page 404 = @not_found;
     location @not_found {
         return 404;
     }
  }
 
and it appears to work. I think the downside of this is that 
I have to have the location /error.php explicitly invoke php.

While I enjoy hacking at something, I also enjoy understanding something
just as well. So what am I confused about?

Thanks in advance. 
-- 
Dave Hayes - Consultant - Altadena CA, USA - dave at jetcafe.org 
>>> The opinions expressed above are entirely my own <<<

"That's a very dangerous lake, Nasrudin. People who swim in
it are always being found dead at the bottom."

"That's alright, friend", replied Nasrudin. "I'll keep well
away from the bottom..."





More information about the nginx mailing list