NGINX – 404redirect不工作,返回一个错误代码500

我最近设置了NGINX,并且必须实施各种redirect来pipe理我们旧网站中仍然返回Google(以及其他各种链接页面)的页面。

其中一些链接,我们必须redirect到明确的位置,但其余的应该被redirect到根目录/。

我被要求将404,500和501页面redirect回根目录。 我认为这将是相当容易实现使用error_page指令。 我的服务器块包含以下内容

 error_page 404 500 501 = @redir; location @redir { rewrite .* / redirect; } 

在我的日志文件中,当我请求随机不存在的文件时,我看到以下内容

xxxx - - [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"

现在提出两个问题:

  1. 为什么这会返回500错误,而不是404?
  2. 为什么500错误是由error_page部分处理的?

这是整个服务器块

 server { listen xxxx: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; } } 

有任何想法吗?

我可以回答我的第一个问题

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!

我仍然不知道为什么500错误不被NGINX拾取。 我注意到500错误仍然返回默认的500.html页面。 对于我来说,我只是做了一个丑陋的元素刷新,但现在工作。

理想情况下,我想要一个适当的redirect。