目的:解决客户端访问公司域名站点出现报错时返回生硬报错代码问题,进行返回错误页面自定制,提供良好用户体验。

本文将依托nginx,从几个角度讲述几个web(nginx、apache、tomcat)服务,如何进行错误页面自定义。


首先梳理一下web服务器常见的错误类型:

④4xx 错误 

400 Bad Request 请求出现语法错误。 


401 Unauthorized 客户试图未经授权访问受密码保护的页面。应答中会包含一个WWW-Authenticate头,浏览器据此显示用户名字/密码对话框,然后在填写合适的Authorization头后再次发出请求。 

403 Forbidden 资源不可用。服务器理解客户的请求,但拒绝处理它。通常由于服务器上文件或目录的权限设置导致。 


404 Not Found 无法找到指定位置的资源。这也是一个常用的应答。 

405 Method Not Allowed 请求方法(GET、POST、HEAD、Delete、PUT、TRACE等)对指定的资源不适用。(HTTP 1.1新) 

406 Not Acceptable 指定的资源已经找到,但它的MIME类型和客户在Accpet头中所指定的不兼容(HTTP 1.1新)。 

407 Proxy Authentication Required 类似于401,表示客户必须先经过代理服务器的授权。(HTTP 1.1新) 

408 Request Timeout 在服务器许可的等待时间内,客户一直没有发出任何请求。客户可以在以后重复同一请求。(HTTP 1.1新) 

409 Conflict 通常和PUT请求有关。由于请求和资源的当前状态相冲突,因此请求不能成功。(HTTP 1.1新) 

410 Gone 所请求的文档已经不再可用,而且服务器不知道应该重定向到哪一个地址。它和404的不同在于,返回407表示文档永久地离开了指定的位置,而404表示由于未知的原因文档不可用。(HTTP 1.1新) 

411 Length Required 服务器不能处理请求,除非客户发送一个Content-Length头。(HTTP 1.1新) 

412 Precondition Failed 请求头中指定的一些前提条件失败(HTTP 1.1新)。 

413 Request Entity Too Large 目标文档的大小超过服务器当前愿意处理的大小。如果服务器认为自己能够稍后再处理该请求,则应该提供一个Retry-After头(HTTP 1.1新)。 

414 Request URI Too Long URI太长(HTTP 1.1新)。 

416 Requested Range Not Satisfiable 服务器不能满足客户在请求中指定的Range头。(HTTP 1.1新) 

⑤5xx 服务器错误 

500 Internal Server Error 服务器遇到了意料不到的情况,不能完成客户的请求。 

501 Not Implemented 服务器不支持实现请求所需要的功能。例如,客户发出了一个服务器不支持的PUT请求。 

502 Bad Gateway 服务器作为网关或者代理时,为了完成请求访问下一个服务器源码天空,但该服务器返回了非法的应答。 

503 Service Unavailable 服务器由于维护或者负载过重未能应答。例如,Servlet可能在数据库连接池已满的情况下返回503。服务器返回503时可以提供一个Retry-After头。 

504 Gateway Timeout 由作为代理或网关的服务器使用,表示不能及时地从远程服务器获得应答。(HTTP 1.1新) 

505 HTTP Version Not Supported 服务器不支持请求中所指明的HTTP版本。(HTTP 1.1新)

http://www.codesky.net/article/201012/149212.html


配置实例:


1.nginx作为域名服务器,提供静态、动态域名服务,定制错误页面返回配置方法;


在域名监听主目录下建立error页面文件夹,将错误页面文件上传到文件夹,nginx配置如下:


#www.xx.jp##静态页面!

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##动态页面!

  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作为反向代理服务器,定制错误页面返回配置方法;

经简单测试,nginx反响代理只能拦截502错误代码!可以将错误页面直接跳转到公司其它服务器的错误页面,配置方法;


       server

       {

               listen  80;

               server_name     www.aa.com;

               location / {

error_page  502 503 504      http://www.xx.com/503.html;   #将返回500系列的错误页面,直接跳转的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

                         }

       }

}


#上面的配置方法,页面会跳转到http://www.xx.com/503.html ,下面的配置遇到错误页面不会跳转,在原有域名请求连接页面展示。


#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 指定一下监听目录(/data/nperror),在监听目录内建立 error文件夹,将错误页面上传的error文件夹内。

       location /error {

              root /data/nperror;

}


         }


3.nginx作为反向代理服务器,apache作为后端服务器,apache自定义错误页面配置如下:

apache默认自带有错误返回页面,默认处于注释状态,如果公司没有特殊要求,只要将注释关掉即可,下面的配置的例子 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作为反向代理服务器,tomcat作为后端服务器,tomcat自定义错误页面配置如下:

将错误页面上传到域名监听目录,tomcat的默认应用放在\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>

本文出自 “康建华” 博客,请务必保留此出处http://michaelkang.blog.51cto.com/1553154/1251192