6

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?

Steffan Donal
  • 71
  • 1
  • 6

5 Answers5

0

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;

    }
  ................
  ................
    } 
0

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;
}
Andrei Mikhaltsov
  • 3,027
  • 1
  • 23
  • 31
0

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)

jeffatrackaid
  • 4,142
  • 19
  • 22
0

Fallback error pages in nginx work like:

error_page 400 /400.html;
location = /400.html {
    try_files /400.html @error;    # <-- this is your missing part 1
    internal;                      # suggested
}
location @error {   # <-- this is your missing part 2
    root /var/www/error;
}
0

Try using an if conditional in your server that handles cases where that website does not exist

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

    if(!-d /usr/share/nginx/sites/$host){ 
        set $host notexist
    }

    root /usr/share/nginx/sites/$host;

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

And add an index.html in the /usr/share/nginx/sites/notexist describing that the site does not exist

Amith KK
  • 101
  • 3