nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri,这个可以是一个自定义错误页,也可以是自定义的错误图片或者后端的响应信息。
5.1 分类
按照跳转类型不同可以分为以下3类:
1、跳转到Nginx本地文件
将400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506指向本地的404.html,这种方式浏览器收到的状态码不变
server{
error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /404.html;
location =/404.html {
root /data/nginx/html;
}
}
实际上产生了一个内部跳转(internal redirect),当访问出现上述错误码的时候就能返回404.html中的内容,这里需要注意是否可以找到404.html页面,所以加了个location保证找到你自定义的404页面。
2、跳转到指定url
将404状态码指向 http://www.liupeng.com/404.html,这种方式浏览器收到的状态码不变
server{
error_page 404 http://www.liupeng.com/404.html;
}
注意:404.html与分号之间也不能有空格。
3、跳转到n