目的:解決客戶端訪問公司域名站點出現(xiàn)報錯時返回生硬報錯代碼問題,進行返回錯誤頁面自定制,提供良好用戶體驗。
本文將依托nginx,從幾個角度講述幾個web(nginx、apache、tomcat)服務(wù),如何進行錯誤頁面自定義。
首先梳理一下web服務(wù)器常見的錯誤類型:
④4xx 錯誤
400 Bad Request 請求出現(xiàn)語法錯誤。
401 Unauthorized 客戶試圖未經(jīng)授權(quán)訪問受密碼保護的頁面。應(yīng)答中會包含一個WWW-Authenticate頭,瀏覽器據(jù)此顯示用戶名字/密碼對話框,然后在填寫合適的Authorization頭后再次發(fā)出請求。
403 Forbidden 資源不可用。服務(wù)器理解客戶的請求,但拒絕處理它。通常由于服務(wù)器上文件或目錄的權(quán)限設(shè)置導致。
404 Not Found 無法找到指定位置的資源。這也是一個常用的應(yīng)答。
405 Method Not Allowed 請求方法(GET、POST、HEAD、Delete、PUT、TRACE等)對指定的資源不適用。(HTTP 1.1新)
406 Not Acceptable 指定的資源已經(jīng)找到,但它的MIME類型和客戶在Accpet頭中所指定的不兼容(HTTP 1.1新)。
407 Proxy Authentication Required 類似于401,表示客戶必須先經(jīng)過代理服務(wù)器的授權(quán)。(HTTP 1.1新)
408 Request Timeout 在服務(wù)器許可的等待時間內(nèi),客戶一直沒有發(fā)出任何請求??蛻艨梢栽谝院笾貜屯徽埱?。(HTTP 1.1新)
409 Conflict 通常和PUT請求有關(guān)。由于請求和資源的當前狀態(tài)相沖突,因此請求不能成功。(HTTP 1.1新)
410 Gone 所請求的文檔已經(jīng)不再可用,而且服務(wù)器不知道應(yīng)該重定向到哪一個地址。它和404的不同在于,返回407表示文檔永久地離開了指定的位置,而404表示由于未知的原因文檔不可用。(HTTP 1.1新)
411 Length Required 服務(wù)器不能處理請求,除非客戶發(fā)送一個Content-Length頭。(HTTP 1.1新)
412 Precondition Failed 請求頭中指定的一些前提條件失敗(HTTP 1.1新)。
413 Request Entity Too Large 目標文檔的大小超過服務(wù)器當前愿意處理的大小。如果服務(wù)器認為自己能夠稍后再處理該請求,則應(yīng)該提供一個Retry-After頭(HTTP 1.1新)。
414 Request URI Too Long URI太長(HTTP 1.1新)。
416 Requested Range Not Satisfiable 服務(wù)器不能滿足客戶在請求中指定的Range頭。(HTTP 1.1新)
⑤5xx 服務(wù)器錯誤
500 Internal Server Error 服務(wù)器遇到了意料不到的情況,不能完成客戶的請求。
501 Not Implemented 服務(wù)器不支持實現(xiàn)請求所需要的功能。例如,客戶發(fā)出了一個服務(wù)器不支持的PUT請求。
502 Bad Gateway 服務(wù)器作為網(wǎng)關(guān)或者代理時,為了完成請求訪問下一個服務(wù)器源碼天空,但該服務(wù)器返回了非法的應(yīng)答。
503 Service Unavailable 服務(wù)器由于維護或者負載過重未能應(yīng)答。例如,Servlet可能在數(shù)據(jù)庫連接池已滿的情況下返回503。服務(wù)器返回503時可以提供一個Retry-After頭。
504 Gateway Timeout 由作為代理或網(wǎng)關(guān)的服務(wù)器使用,表示不能及時地從遠程服務(wù)器獲得應(yīng)答。(HTTP 1.1新)
505 HTTP Version Not Supported 服務(wù)器不支持請求中所指明的HTTP版本。(HTTP 1.1新)
http://www.codesky.net/article/201012/149212.html
配置實例:
1.nginx作為域名服務(wù)器,提供靜態(tài)、動態(tài)域名服務(wù),定制錯誤頁面返回配置方法;
在域名監(jiān)聽主目錄下建立error頁面文件夾,將錯誤頁面文件上傳到文件夾,nginx配置如下:
#www.xx.jp##靜態(tài)頁面!
server {
listen 80;
server_name www.xx.com;
location / {
root /home/wwwroot/www.xx.com/;
index index.html index.htm;
# redirecet server error pages to the static pag !
error_page 401 = /error/401.html;
error_page 403 = /error/403.html;
error_page 404 = /error/404.html;
error_page 500 = /error/500.html;
error_page 502 503 504 = /error/503.html;
}
}
#www.xx.jp##動態(tài)頁面!
server {
listen 80;
server_name www.xx.com;
location / {
root /home/wwwroot/www.xx.com/;
index index.php index.html index.htm;
# redirecet server error pages to the static pag !
# redirecet server error pages to the static pag /50x.html
error_page 400 /error/400.html;
error_page 401 /error/401.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 408 /error/408.html;
error_page 500 /error/500.html;
error_page 502 503 504 /error/502.html;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
}
2.nginx作為反向代理服務(wù)器,定制錯誤頁面返回配置方法;
經(jīng)簡單測試,nginx反響代理只能攔截502錯誤代碼!可以將錯誤頁面直接跳轉(zhuǎn)到公司其它服務(wù)器的錯誤頁面,配置方法;
server
{
listen 80;
server_name www.aa.com;
location / {
error_page 502 503 504 http://www.xx.com/503.html; #將返回500系列的錯誤頁面,直接跳轉(zhuǎn)的www.xx.com的503錯誤頁面(此頁面要真實存在)。
# proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_pass http://172.17.4.11:6666;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
中間省略********************88
}
}
}
#上面的配置方法,頁面會跳轉(zhuǎn)到http://www.xx.com/503.html ,下面的配置遇到錯誤頁面不會跳轉(zhuǎn),在原有域名請求連接頁面展示。
#port 80
server
{
listen 80;
server_name tz1.xx.com;
proxy_redirect off;
location / {
# proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_pass http://192.168.18.72:10080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
中間省略********************88
# redirecet server error pages to the static pag !
error_page 408 /error/408.
error_page 500 /error/500.html;
error_page 502 503 504 /error/503.html;
}
#新建一個location 指定一下監(jiān)聽目錄(/data/nperror),在監(jiān)聽目錄內(nèi)建立 error文件夾,將錯誤頁面上傳的error文件夾內(nèi)。
location /error {
root /data/nperror;
}
}
3.nginx作為反向代理服務(wù)器,apache作為后端服務(wù)器,apache自定義錯誤頁面配置如下:
apache默認自帶有錯誤返回頁面,默認處于注釋狀態(tài),如果公司沒有特殊要求,只要將注釋關(guān)掉即可,下面的配置的例子 874~878行是我自己添加的(將錯誤頁面文件上傳到/var/www/error),返回的頁面是我們公司自己定制頁面,將下面的錯誤代碼行進行注釋。其它的錯誤頁面保持默認。
874 ErrorDocument 401 /error/401.html
875 ErrorDocument 403 /error/403.html
876 ErrorDocument 404 /error/404.html
877 ErrorDocument 500 /error/500.html
878 ErrorDocument 503 /error/503.html
879
880 ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
881 # ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
882 # ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
883 # ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
884 ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
885 ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
886 ErrorDocument 410 /error/HTTP_GONE.html.var
887 ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
888 ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
889 ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
890 ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
891 ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
892 # ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
893 ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
894 ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
895 # ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
896 ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var
4.nginx作為反向代理服務(wù)器,tomcat作為后端服務(wù)器,tomcat自定義錯誤頁面配置如下:
將錯誤頁面上傳到域名監(jiān)聽目錄,tomcat的默認應(yīng)用放在\webapps\ROOT目錄里:修改webapps\ROOT\WEB-INF\web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!--配置以下代碼-->
<error-page>
<error-code>404</error-code>
<location>/錯誤頁面的存放路徑/錯誤頁面名稱.jsp</location>
</error-page>
</web-app>
多個錯誤頁面依次往下寫就行了!
<error-page>
<error-code>400</error-code>
<location>/400.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>