# 404 problem

**URL:** <https://www.ruby-forum.com/t/404-problem/209301>\
**Category:** NGINX\
**Created:** [July 11, 2011, 8:16am UTC](https://www.ruby-forum.com/t/404-problem/209301 "2011-07-11T08:16:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 11, 2011, 8:16am UTC](https://www.ruby-forum.com/t/404-problem/209301/1 "2011-07-11T08:16:11Z")

</div>

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](http://domain.com/not-exist.html); but it will not work  
for [domain.com/sub-dir/not-exist.html](http://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:

> **[404 problem](https://forum.nginx.org/read.php?2,212060,212060#msg-212060)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 11, 2011, 9:03am UTC](https://www.ruby-forum.com/t/404-problem/209301/2 "2011-07-11T09:03:44Z")

</div>

On Mon, Jul 11, 2011 at 1:15 PM, etrader [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/ {
    ...
  }
  ..
}

```

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 11, 2011, 9:21am UTC](https://www.ruby-forum.com/t/404-problem/209301/3 "2011-07-11T09:21:01Z")

</div>

There is no real sub-directory, they are virtual folders created by  
rewrites; e.g. [domain.com/search/keyword.html](http://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:

> **[Re: 404 problem](https://forum.nginx.org/read.php?2,212060,212063#msg-212063)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 11, 2011, 9:37am UTC](https://www.ruby-forum.com/t/404-problem/209301/4 "2011-07-11T09:37:05Z")

</div>

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

Posted at Nginx Forum:

> **[Re: 404 problem](https://forum.nginx.org/read.php?2,212060,212065#msg-212065)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 11, 2011, 9:50am UTC](https://www.ruby-forum.com/t/404-problem/209301/5 "2011-07-11T09:50:05Z")

</div>

On Mon, Jul 11, 2011 at 2:36 PM, etrader [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

as in empty response?

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 11, 2011, 9:27am UTC](https://www.ruby-forum.com/t/404-problem/209301/6 "2011-07-11T09:27:11Z")

</div>

On Mon, Jul 11, 2011 at 2:20 PM, etrader [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is no real sub-directory, they are virtual folders created by  
> rewrites; e.g. [domain.com/search/keyword.html](http://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?
