custom 404 nginx + php-fpm

Igor Sysoev is at rambler-co.ru
Wed Jul 29 13:06:02 MSD 2009


On Wed, Jul 29, 2009 at 04:49:10AM -0400, shaktale wrote:

> Hi all.
> 
> I have the folllowing config:
> 
>   
> # static files without rewrite
>   location ~ ^.+\.(js|jpe?g|gif|png|css)$ {
>         # 404 
>         try_files  $uri /index.php;
>         expires 1d;
>   }
> 
>   location / {
>      rewrite ^/(.+)_(.+)/(.+)/$ /index.php/action1/$1/action2/$2/action3/$3 last;
>      rewrite ^(/.+_.+/.+)([^/])$ $1$2/ permanent;
> 
>      try_files $uri /index.php?$args;
>   }
> 
>   # PHP
>   location ~ ^(.+\.php.*)$ {
>     fastcgi_split_path_info         ^(.+\.php)(.*)$;
>     fastcgi_pass   127.0.0.1:9000;
> 
>     include /usr/local/nginx/conf/fastcgi_params;
>     fastcgi_param  PATH_INFO        $fastcgi_path_info;
>     fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
>   }
> 
> 
> I want to handle 404 errors with application (index.php). 
> 
> Works fine, except urls with php files, ie mydomain.com/foo.php
> 
> The foo.php is passed to fcgi, and php-fpm returns "No input file specified." 
> 
> If I use try_file in the "location ~ ^(.+\.php.*)$", then, the rewrites stops working. An error_page does not work either.
> 
> Any ideas ? 

Probably yuo need somthing like this:

   location ~ ^.+\.(js|jpe?g|gif|png|css)$ {
        try_files  $uri /index.php;
        expires 1d;
   }

   loaction ~ ^/(.+)_(.+)/(.+)/$ {
        fastcgi_pass   127.0.0.1:9000;
        include /usr/local/nginx/conf/fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  /path/to/index.php;
        fastcgi_param  SCRIPT_NAME      /index.php;
        fastcgi_param  PATH_INFO        /action1/$1/action2/$2/action3/$3;
   }

   location ~ ^(/.+_.+/.+)([^/])$ {
       rewrite ^(/.+_.+/.+)([^/])$ $1$2/ permanent;
   }

   location ~ ^.+\.php$ {
       try_files $uri /index.php?$args;

       fastcgi_pass   127.0.0.1:9000;
       include /usr/local/nginx/conf/fastcgi_params;
       fastcgi_param  SCRIPT_FILENAME  /path/to$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
   }

   location / {
       try_files $uri /index.php?$args;
   }


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





More information about the nginx mailing list