WEB服务与NGINX(7)-实现自定义错误页面

1. 自定义错误页面

  • error_page code ... [=[response]] uri;

    定义错误页,以指定的响应状态码进行响应,此指令由ngx_http_index_module模块提供

    支持环境:http, server, location, if in location

    #1.修改nginx的配置文件如下,定义错误页,把500 502 503 504 404这些状态码转换为200状态码,并定义错误页面。
    [root@nginx01 ~]# vim /etc/nginx/conf.d/virtualhost.conf 
    server {
    	listen 80;
    	server_name www.nginx01.com;
    	location / {
    		root /data/nginx/html/web1;
    		index index.html;
    	}
    
    	error_page 500 502 503 504 404 =200 /errorpage.html;
    	location = /errorpage.html {
    		root /data/nginx/html/web1/errorpage/;
    	}
    }
    
    #2.重启nginx服务
    [root@nginx01 web1]# systemctl reload nginx.service
    
    #3.在web1下新建目录errorpage和文件errorpage/errorpage.html
    [root@nginx01 web1]# ll errorpage/
    total 4
    -rw-r--r-- 1 root root 31 Jun 15 23:30 errorpage.html
    [root@nginx01 web1]# cat errorpage/errorpage.html 
    our system is being maintained
    
    #4.使用客户端访问一个不存在的页面,能正常返回自定义的错误页面,状态码变为200
    [root@xuzhichao ~]# curl http://www.nginx01.com/abc
    our system is being maintained
    [root@xuzhichao ~]# curl -I http://www.nginx01.com/abc
    HTTP/1.1 200 OK
    Server: nginx/1.20.1
    Date: Tue, 15 Jun 2021 15:36:27 GMT
    Content-Type: text/html
    Content-Length: 31
    Last-Modified: Tue, 15 Jun 2021 15:30:38 GMT
    Connection: keep-alive
    ETag: "60c8c79e-1f"
    Accept-Ranges: bytes
posted @ 2021-06-19 23:18  向往自由的独行者  阅读(1320)  评论(0编辑  收藏  举报