Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have set up an any-domain config on my nginx server - to reduce the amount of work needed when I open a new site/domain. This config allows me to simply create a folder in /usr/share/nginx/sites/ with the name of the domain/subdomain and then it just works.™

server {
    # Catch all domains starting with only "www." and boot them to non "www." domain.
    listen 80;
    server_name ~^www\.(.*)$;
    return 301 $scheme://$1$request_uri;
}

server {
    # Catch all domains that do not start with "www."
    listen 80;
    server_name ~^(?!www\.).+;
    client_max_body_size 20M;

    # Send all requests to the appropriate host
    root /usr/share/nginx/sites/$host;

    index index.html index.htm index.php;
    location / { 
        try_files $uri $uri/ =404;
    }

    recursive_error_pages on;
    error_page 400 /errorpages/error.php?e=400&u=$uri&h=$host&s=$scheme;
    error_page 401 /errorpages/error.php?e=401&u=$uri&h=$host&s=$scheme;
    error_page 403 /errorpages/error.php?e=403&u=$uri&h=$host&s=$scheme;
    error_page 404 /errorpages/error.php?e=404&u=$uri&h=$host&s=$scheme;
    error_page 418 /errorpages/error.php?e=418&u=$uri&h=$host&s=$scheme;
    error_page 500 /errorpages/error.php?e=500&u=$uri&h=$host&s=$scheme;
    error_page 501 /errorpages/error.php?e=501&u=$uri&h=$host&s=$scheme;
    error_page 503 /errorpages/error.php?e=503&u=$uri&h=$host&s=$scheme;
    error_page 504 /errorpages/error.php?e=504&u=$uri&h=$host&s=$scheme;

    location ~ \.(php|html) {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_intercept_errors on;
    }
}

However there is one issue that I'd like to resolve, and that is when a domain that doesn't have a folder in the sites directory, nginx throws an internal 500 error page because it cannot redirect to /errorpages/error.php as it doesn't exist.

How can I create a fallback error page that will catch these failed requests?

share|improve this question
    
Do the sites have different error.php code or is it the same? –  jeffatrackaid May 31 '13 at 19:35
    
All different, unfortunately –  Ruirize Jun 6 '13 at 13:29

3 Answers 3

Below piece of code should solve your issue

  http {
      server {
          error_page 404 http://www.mywebsite.com/error.php;
          error_page 500 http://www.mywebsite.com/error.php;

    }
  ................
  ................
    } 
share|improve this answer
    
The problem with this is that it doesn't allow for custom error pages per domain. –  Ruirize May 22 '13 at 11:34
    
How about using $server_name variable inside a path? For example, error_page 404 $scheme://$server_name/error.php –  jollyroger May 29 '13 at 18:22
    
Only just seen this comment. Will try. –  Ruirize May 31 '13 at 18:49
    
No, this produces the same result. –  Ruirize May 31 '13 at 18:56

May be this will work: (instead of return 404 in last location you can try to specify exact error URL)

location / { 
        try_files $uri $uri/ @notfound;
    }

location @notfound {
  root /usr/share/nginx/sites/errorpages;
  return 404;
}
share|improve this answer
    
This doesn't cover any other errors. –  Ruirize May 31 '13 at 18:49
    
Yes, but how can other errors happen with non-existent $host? i think they all will be 404 –  Andrei Mikhaltsov Jun 1 '13 at 21:43
    
For some reason, nginx still attempts to redirect, causing an internal error. –  Ruirize Jun 4 '13 at 9:51

I probably need to give this more thought, but what if you try something like:

location /errorpages {
try_files $uri $uri/  @fallback
}

location @fallback {
root /usr/share/nginx/sites/default
... error page directives ...

}

Where the default holds the error pages for non-existent sites.

Basically try to create a conditional root.

See: http://end.re/2011/05/02/nginx-alternative-root-with-conditional-autoindex/

Update

This worked for the 404 case:

location / 
try_files $uri $uri/ @errors;
...
}

location @errors {
try_files $uri $uri/ =510;
root /usr/share/nginx/sites/errors;
error_page 404 = /404.html
}

(You have to create the errors/404.html of course)

share|improve this answer
    
The root change appears to not work - it still causes an internal redirection cycle. –  Ruirize Jun 4 '13 at 9:51
    
I updated my answer with something that may get you partway. –  jeffatrackaid Jun 5 '13 at 19:48
    
It's still ignoring the root change. –  Ruirize Jun 6 '13 at 13:29
    
You may need to pull your error directives from the server block. I don't have time to retest but pretty sure this works without other error directives. –  jeffatrackaid Jun 6 '13 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.