error_page and named locations

Igor Sysoev is at rambler-co.ru
Thu Dec 11 15:31:34 MSK 2008


On Thu, Dec 11, 2008 at 02:09:25PM +0300, Igor Sysoev wrote:

> On Thu, Dec 11, 2008 at 01:51:51PM +0300, Igor Sysoev wrote:
> 
> > On Thu, Dec 11, 2008 at 10:09:56AM +0000, cynix wrote:
> > 
> > > I'd also like your opinion on another way to do the same thing.
> > > 
> > >  location ~ \.php$ {
> > >   set $script_filename $document_root$fastcgi_script_name;
> > >   if (!-f $script_filename) {
> > >    set $script_filename $document_root/index.php;
> > >   }
> > >   fastcgi_pass   127.0.0.1:1234;
> > >   include        fastcgi_params;
> > >   fastcgi_param  SCRIPT_FILENAME $script_filename;
> > >  }
> > > 
> > > Is this a better way to handle requests to non-existent PHP files? This way
> > > nginx takes care of checking if the file exists, and if it doesn't exist the
> > > original request is not passed to FastCGI at all. Only 1 request is passed to
> > > FastCGI, but with the addition of file existence checking. Will this yield
> > > better performance than 2 requests to FastCGI?
> > 
> > I do not recommend to use if/rewrite in nginx as they have some
> > implementation issues. Nevertheless, your way is better than passing
> > 2 requests to FastCGI.
> > 
> > I'm going to implement the non_existant_request_file directive to use
> > in similar configurations:
> > 
> >     location ~\.php$ {
> >         non_existant_request_file  @drupal;
> > 
> >         fastcgi_pass  ..
> >     }
> > 
> > This directive test a request file existance before passing a request
> > to fastcgi/proxy.
> > 
> > Could someone suggest better name ?
> 
> Also I want to replace typcal mongrel configuration:
> 
>   location / {
> 
>       if (-f $request_filename) {
>           break;
>       }
> 
>       if (-f $request_filename/index.html) {
>           rewrite (.*) $1/index.html break;
>       }
> 
>       if (-f $request_filename.html) {
>           rewrite (.*) $1.html break;
>       }
> 
>       if (!-f $request_filename) {
>           proxy_pass http://mongrel;
>           break;
>       }
>   }
> 
> with something like
> 
>      location / {
>          match  $request_filename
>                 $request_filename/index.html
>                 $request_filename.html;
>                 @mongrel;
>      }
> 
> like "index" directive.
> 
> Or
> 
>          try    $request_filename
>                 $request_filename/index.html
>                 $request_filename.html
>                 /some_fallback_url;

The single directive can be used for Mongrel type configuration:

      location / {
          file_match  $uri  $uri/index.html  $uri.html
                      @mongrel;
      }

      location @mongrel {
          ...
      }

and Drupal/Joomla type configuration:

      location / {
          file_match  $uri  @drupal;
          # the same as
          #    error_page  404 = @drupal; log_not_found off;
      }

      location ~ \.php$ {
          file_match  $uri  @drupal;

          fastcgi_pass   ...
          fastcgi_param  SCRIPT_FILENAME /path/to$script_filename;
      }

      location @drupal {
          fastcgi_pass   ...
          fastcgi_param  SCRIPT_FILENAME /path/to/index.php;
      }

The file_match iterates parameters and changes URI to the first one
that matches filesystem. An @name matches always and is used as fallback.
All matches except fallback are handled in the same location context.


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list