天天看點

nginx tomcat 404 頁面跳轉

今天業務需求需要把所有的404頁面都跳轉到一個固定頁面,下面說下我的生産環境

Nginx+Tomcat實作反向代理,當Client請求某個action的時候如果tomcat抛出404,那麼就指定nginx通路到一個首頁。

使用反向代理的生産環境需要使用下面這個參數

在server段中加如下參數

proxy_intercept_errors on

文法:proxy_intercept_errors |關閉;

預設值:

proxy_intercept_errors;

上下文:http伺服器的位置

當被代理的後端伺服器的響應狀态碼大于等于300時,決定是否直接将響應發送給用戶端,亦或将響應轉發給nginx由error_page指令來處理。

原文:

syntax:proxy_intercept_errors on | off;

default:

proxy_intercept_errors off;

context:http, server, location

Determines whether proxied responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.

proxy_intercept_errors 為on 表示 nginx按照原response code 輸出,後端是404,就是404.這個變量開啟後,我們才能自定義錯誤頁面。

nginx配置檔案代碼:

文法:fastcgi_intercept_errors on | off;

fastcgi_intercept_errors off;

上下文:http, server, location

當FastCGI後端伺服器響應狀态碼大于等于300時,決定是否直接将響應發送給後端用戶端,或者将響應轉發給nginx由 error_page指令來處理。

syntax:fastcgi_intercept_errors on | off;

Determines whether FastCGI server responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.

fastcgi_intercept_errors on表示接收fastcgi輸出的http 1.0 response code,後端php可以輸出header訓示nginx輸出什麼錯誤頁面。開啟這個之後,我們才能在php裡面自定義錯誤代碼和頁面。

必須明确的在error_page中指定處理方法使這個參數有效,如果沒有适當的處理方法,nginx不會攔截一個錯誤,這個錯誤不會顯示自己的預設頁面,這裡允許通過某些方法攔截錯誤。

本文轉自 xinsir999 51CTO部落格,原文連結:http://blog.51cto.com/xinsir/1956550,如需轉載請自行聯系原作者