0

I'd like to have a shared 404 page to use across my virtual hosts. The following is my setup.

Two sites, each with their own config file in /sites-available/ and /sites-enabled/

  1. www.foo.com
  2. bar.foo.com

The www directory is set up as:

www/
foo.com/
foo.com/index.html
bar.foo.com/
bar.foo.com/index.html
shared/
shared/404.html

Both config files in /sites-available are the same except for the root and server name:

root /var/www/bar.foo.com;
index index.html index.htm index.php;

server_name bar.foo.com;

location / {
    try_files $uri $uri/ /index.php;
}
error_page 404 /404.html;
location = /404.html {
    root /var/www/shared;
}

I've tried the above code and also tried setting error_page 404 /var/www/shared/404.html (without the following location block).

I've also double checked to make sure my permissions are set to 775 for all folders and files in www.

When I try to access a non-existent page, Nginx serves the respective index.php of the virtual host I'm trying to access.

Can anyone point out what I'm doing wrong? Thanks!

Choy
  • 169
  • 2
  • 9
  • http://wiki.nginx.org/HttpCoreModule#try_files – quanta Sep 09 '12 at 15:14
  • Not sure I understand. Is there something wrong with the try_files line? It should only serve index.php if I try to visit a directory. If I try to visit http://www.foo.com/doesnotexist.html, it shouldn't return index.php. If I remove the last location block after error_page and move 404.html into one of the site folders, it correctly return the 404 page. – Choy Sep 09 '12 at 15:27
  • _Note that you can specify an HTTP `status` code as the last argument to try_file since Nginx version 0.7.51._ – quanta Sep 09 '12 at 15:43
  • But that would conflict with the index.php catch so if someone tried to visit foo.com/ they'd either get the a 404 page or index.php with a 404 code. – Choy Sep 09 '12 at 15:53
  • Have you tried this yet? – quanta Sep 09 '12 at 15:55
  • I tried try_files $uri $uri/ /index.php /var/www/shared/404.html =404; and try_files $uri $uri/ /var/www/shared/404.html =404;. Both of them just served index.php and tried to download the page if if specified a .html extension. – Choy Sep 09 '12 at 16:05

1 Answers1

1

How about this?

location / {
    try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
    root /var/www/shared;
}
quanta
  • 50,327
  • 19
  • 152
  • 213