404 problem

I set error page as
error_page 404 = 404.php;
or
error_page 404 = /404.php;

in both cases, it will load 404.php when visiting a non-existent page in
the main directory as domain.com/not-exist.html; but it will not work
for domain.com/sub-dir/not-exist.html

How I can set Nginx to redirect to 404.php when visiting any missing
page?

Posted at Nginx Forum:

On Mon, Jul 11, 2011 at 1:15 PM, etrader [email protected] wrote:

page?

assuming you put that error_page in location / { ... } and you have
another location /sub-dir/ { ... }, try moving error_page to server { ... }.

instead of:

server {
  location / {
    error_page ...
    ...
  }
  location /sub-dir/ {
    ...
  }
  ..
}

use:

server {
  error_page ...
  location / {
    ...
  }
  location /sub-dir/ {
    ...
  }
  ..
}

There is no real sub-directory, they are virtual folders created by
rewrites; e.g. domain.com/search/keyword.html instead of
search.php?q=keyword

Config is as

server { server_name .domain.com;
listen 80;
root /home/domain.com;
error_page 404 = /404.php;
index index.html index.php;
rewrite ^/search/(.*).html /search.php?q=$1 last;

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /opt/nginx/conf/fastcgi_params;
}
}

Posted at Nginx Forum:

When I visit a page like domain.com/not-found.html it will redirects to
/404.php
but when visiting a page like domain.com/second/not-found.html it
returns nothing at all (browser not found page)

Posted at Nginx Forum:

On Mon, Jul 11, 2011 at 2:36 PM, etrader [email protected] wrote:

When I visit a page like domain.com/not-found.html it will redirects to
/404.php
but when visiting a page like domain.com/second/not-found.html it
returns nothing at all (browser not found page)

as in empty response?

On Mon, Jul 11, 2011 at 2:20 PM, etrader [email protected] wrote:

There is no real sub-directory, they are virtual folders created by
rewrites; e.g. domain.com/search/keyword.html instead of
search.php?q=keyword

what’s shown? default nginx error page or no input file? Is that the
entire server block?