NGINX – 404 redirect not working, returning an error code 500

nginx

I've recently setup NGINX and had to implement various redirects to manage pages from our older websites that were still returning hits in Google (and other various linked pages).

Some of these links we have to redirect to explicit locations but the rest should just be redirected to the root directory /.

I have been asked to redirect any 404, 500 and 501 pages back to the root directory. I thought this would be fairly easy to achieve using the error_page directive. My server block contains the following

error_page 404 500 501 = @redir;

location @redir
{
    rewrite .* / redirect;
}

In my log files I see the following when I request random non existant files

x.x.x.x - - [02/Mar/2012:11:25:42 +0000] "GET /fgfg.jpg HTTP/1.1" 500 799 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11"

This now brings up two questions:

  1. Why is this returning a 500 error rather than a 404?
  2. Why isn't the 500 redirecting as that's being handled by the error_page section??

Here is the whole server block

server
{
    listen       x.x.x.x:80;
    server_name  www.blah.com;

    access_log  /var/www/www.blah.com/log/access.log;
    error_log   /var/www/www.blah.com/log/error.log;

    root /var/www/www.blah.com/public;
    passenger_enabled on;

    error_page 404 500 501 = @redir;

    location @redir
    {
        rewrite .* / redirect;
    }

    location ~* "/Managed Services"
    {
        rewrite .* /managed_services permanent;
    }

    location ~* /solutions_arch.asp
    {
        rewrite .* /arch permanent;
    }

    location ~* .*\.(php|asp)$
    {
        rewrite .* / permanent;
    }
}

Any ideas?

Best Answer

I can answer my first question

It turns out the reason this wasn't working was because the requests were being passed through to rails and then the default controller was returning an error 500 because the view/page didn't exist!

I still don't know why the 500 error wasn't being picked up by NGINX. I did notice that the 500 error is still returning the default 500.html page. For me to work around it I've just done a meta refresh which is ugly but works for now.

Ideally I'd like a proper redirect though.