2012-01-03 27 views
30

我有一個Sinatra應用程序託管與Unicorn和nginx在它之前。當Sinatra應用程序出錯(返回500)時,我想提供靜態頁面,而不是默認的「內部服務器錯誤」。我有以下nginx的配置:nginx沒有爲我的error_page服務

server { 
    listen 80 default; 
    server_name *.example.com; 
    root /home/deploy/www-frontend/current/public; 

    location/{ 
    proxy_pass_header Server; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Scheme $scheme; 
    proxy_connect_timeout 5; 
    proxy_read_timeout 240; 
    proxy_pass http://127.0.0.1:4701/; 
    } 

    error_page 500 502 503 504 /50x.html; 
} 

的error_page指令是存在的,我已經sudo'd作爲WWW的數據(Ubuntu的),並驗證我可以cat文件,因此它不是一個權限問題。通過上面的配置文件和service nginx reload,我收到的錯誤頁面仍然是「內部服務器錯誤」。

我的錯誤是什麼?

回答

60

error_page可以處理由nginx生成的錯誤。默認情況下,無論http狀態碼如何,nginx都會返回代理服務器返回的任何內容。

你在找什麼是proxy_intercept_errors

該指令決定是否將nginx的具有攔截的400和更高的HTTP 狀態碼響應。

默認情況下,所有響應將從代理服務器按原樣發送。

如果將此設置爲on,則nginx將截取由error_page指令顯式處理的狀態碼 。狀態爲 的代碼與代理服務器的代碼 原樣發送,該代碼與error_page指令不匹配。

+1

我知道這是一個RTFM的問題。感謝您花時間提供一個很好的答案! – 2012-01-03 17:10:02

+9

對4年前的響應做一個快速提示 - 現在'proxy_intercept_errors'適用於等於或大於300的錯誤。 – Tisho 2016-02-25 10:09:56

12

您可以專門設置proxy_intercept_errors該位置

location /some/location { 
    proxy_pass_header Server; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Scheme $scheme; 
    proxy_connect_timeout 5; 
    proxy_read_timeout 240; 
    proxy_pass http://127.0.0.1:4701/; 
    proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors 

    error_page 400 500 404 ... other statuses ... =200 /your/path/for/custom/errors; 
} 

,你可以設置不是200其他身份你需要什麼

+5

使用'proxy_intercept_errors;'(不帶參數)在當前的nginx中不再有效。改用'; proxy_intercept_errors on;'代替。 – Marian 2014-12-02 07:47:16

+0

嗨,瑪麗安,感謝您獲取 – Alexey 2014-12-02 15:47:12

+0

快速的問題關於這裏的語義...設置'proxy_intercept_errors'爲'on'意味着返回自定義頁面,並設置爲'關閉'意味着nginx頁面返回,是否正確? – speedplane 2016-06-14 01:20:56

0

正如斯蒂芬in this response提到,使用proxy_intercept_errors on;可以工作。 雖然在我的情況下,看到in this answer,使用uwsgi_intercept_errors on;的伎倆......

0

人們誰是使用FastCGI作爲其上游需要此參數開啓

fastcgi_intercept_errors on; 

對於我的PHP應用程序,我使用它在我的上游配置塊

location ~ .php$ { ## Execute PHP scripts 
    fastcgi_pass php-upstream; 
    fastcgi_intercept_errors on; 
    error_page 500 /500.html; 
}