- 任何接口都是可能出错,4xx、5xx等
- 如果业务没有做好统一的错误管理,直接暴露给用户,无疑是看不懂
- 所以假如后端某个业务出错,nginx层也需要进行转换
- 让前端知道http响应是200,其实是将错误的状态码定向至200,返回了全局兜底数据
upstream lbs {
server 192.168.1.12:8080 max_fails=2 fail_timeout=60s;
server 192.168.1.12:8081 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost a.com;
location / {
proxy_pass http://lbs;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_503 http_404;
proxy_intercept_errors on;
}
error_page 404 500 502 503 504 =200 /default_api;
location = /default_api {
default_type application/json;
return 200 '{"code":"-99","msg":"error"}';
}
}