46 Nginx的error_page指令

46 Nginx的error_page指令

46.1 error_page指令

error_page:设置网站的错误页面

语法 error_page code ... [=[response]] uri;
默认值 -
位置 http 、server 、location

 

 

 

 

code 是后端响应给前端的状态码

当出现对应的响应 code 后,如何来处理?

46.2 跳转

指定具体跳转的地址

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
          index mv.png;
        }
        error_page 404 http://www.baidu.com;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/aaa ,没有配置 aaa,应该返回404,会自动跳转到 www.baidu.com

image

46.3 重定向

指定重定向的地址,nginx 默认采用的规则

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
          index mv.png;
        }
        #error_page 404 http://www.baidu.com;
        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/aaa ,没有配置 aaa,应该返回404,会重定向 /50x.html,从而被下面的 location 匹配

image

46.4 location @

使用 location 的 @ 符合完成错误信息展示

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
          index mv.png;
        }
        #error_page 404 http://www.baidu.com;

        error_page 404 @jump_to_error;
        location @jump_to_error {
           default_type text/plain;
           return 404 'Not Found HTML Page';
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/aaa 

image

46.5 =[response]

可选:=[response] 的作用是用来将相应状态码更改为另外一个

前端收集到返回404后,返回了正常的内容,可以使用正常状态码 200 返回

image

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
          index mv.png;
        }
        #error_page 404 http://www.baidu.com;

        #error_page 404 @jump_to_error;
        #location @jump_to_error {
        #   default_type text/plain;
        #   return 404 'Not Found HTML Page';
        #}
        
        error_page 404 =200 /50x.html;

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器状态码 200

image

当返回404,找不到对应资源时,在浏览器上可以看到,但最终返回状态码是 200

注意:编写 error_page 后面的内容,404 后面需要加空格,200 前面不能加空格

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-12 11:42  马俊南  阅读(4)  评论(0)    收藏  举报