
在开始配置之前,我们需要确保系统中已经安装了 Nginx。如果尚未安装,请根据你的操作系统执行以下命令进行安装。本指南以 CentOS 7 为例,Ubuntu 用户请自行替换 apt-get 相关命令。
安装 Nginx:
yum install epel-release -y
yum install nginx -y
安装完成后,首先备份原始配置文件,防止配置错误导致服务无法启动。
备份配置文件:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
本方案的核心思路是利用 Nginx 的 proxy_next_upstream 实现故障自动重试,配合 error_page 和 proxy_intercept_errors 实现重试失败后的页面降级。为了方便测试,我们将在同一台 Nginx 上配置两个端口:80 端口作为反向代理层(防护层),8080 端口模拟后端应用服务(故障源)。
为了让你能立即看到效果,我们先配置一个模拟后端。请在 /etc/nginx/nginx.conf 的 http 块中添加以下 server 块。这个服务监听 8080 端口,并配置为直接返回 500 错误,模拟应用服务器崩溃或代码报错的场景。
server {
listen 8080;
server_name localhost;
location / {
模拟后端应用发生500错误
return 500 'Internal Server Error: Backend is down.';
}
}
保存配置后,执行 nginx -t 检查语法,无误后执行 systemctl restart nginx 重启服务。此时访问 http://你的服务器IP:8080 应该能看到 500 错误页面。
接下来是本次实操的核心部分。我们将配置 80 端口作为对外服务的入口。该配置包含三个关键动作:检测到 500 错误时自动重试、重试次数限制、彻底失败后拦截错误并返回降级页面。
请打开 /etc/nginx/nginx.conf,在 http 块中添加或修改以下内容:
upstream backend_servers {
这里填写你的真实后端地址,为了演示我们指向本地的8080故障端口
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
keepalive 32;
}
server {
listen 80;
server_name localhost;
开启代理错误拦截,这是关键,否则Nginx会将后端的500直接透传给用户
proxy_intercept_errors on;
location / {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
proxy_set_header Connection "";
核心配置:定义哪些情况下需要重试
error: 连接错误 timeout: 超时 http_500: 后端返回500 http_502: 后端返回502
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
限制重试次数,防止无限重试导致雪崩
proxy_next_upstream_tries 2;
设置连接和读取超时时间,单位秒
proxy_connect_timeout 5s;
proxy_read_timeout 5s;
proxy_send_timeout 5s;
}
定义500错误的处理逻辑,指向降级页面
这里的 =200 表示将响应状态码强制修改为200,对SEO和用户更友好
error_page 500 502 503 504 =200 /50x.html;
降级页面的具体路径
location = /50x.html {
root /usr/share/nginx/html;
}
}
配置项详解:

配置文件中引用了 /usr/share/nginx/html/50x.html,我们需要确保这个文件存在且内容友好。默认 Nginx 安装包会自带这个文件,但内容通常比较简陋。我们可以将其修改为更有用的提示信息。
执行以下命令覆盖默认降级页面:
cat > /usr/share/nginx/html/50x.html << 'EOF'
系统维护中
服务暂时不可用
系统正在繁忙处理请求,请稍后刷新页面重试。
技术团队已收到通知,正在紧急修复。
EOF
对于前后端分离的项目,返回 HTML 页面会导致前端 JSON 解析失败。我们需要针对 API 接口返回 JSON 格式的降级数据。我们需要修改 80 端口的 server 配置,增加一个专门处理 API 降级的 location。
修改 /etc/nginx/nginx.conf,在 http 块中添加如下配置:
server {
listen 80;
server_name localhost;
proxy_intercept_errors on;
定义一个命名location,用于返回JSON降级数据
location @api_fallback {
default_type application/json;
return 200 '{"code": -1, "msg": "服务繁忙,请稍后重试", "data": null}';
}
location /api/ {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_next_upstream_tries 2;
针对API接口,错误时跳转到内部的 @api_fallback location
error_page 500 502 503 504 =200 @api_fallback;
}
location / {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_next_upstream_tries 2;
普通页面请求,跳转到静态HTML
error_page 500 502 503 504 =200 /50x.html;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
注意:location 的匹配顺序非常重要。请确保 /api/ 的 location 块放在通配的 / location 块之前,否则通配规则会优先匹配,导致 API 降级不生效。
配置修改完成后,执行以下命令重载配置:
nginx -t && systemctl reload nginx
现在我们开始验证防护效果。
1. 验证页面降级:

在浏览器或终端访问:
curl -i http://127.0.0.1/
预期结果:HTTP 状态码为 200 OK,Body 内容为我们刚才配置的“系统维护中”HTML 页面。查看 Nginx error log(tail -f /var/log/nginx/error.log),可以看到 upstream 返回 500 的记录,但用户收到的却是 200。
2. 验证接口降级:
访问 API 地址:
curl -i http://127.0.0.1/api/userinfo
预期结果:HTTP 状态码为 200 OK,Content-Type 为 application/json,Body 内容为 JSON 字符串 {"code": -1, ...}。
3. 验证重试机制:
观察 error log,如果后端一直返回 500,你会看到对于同一个请求,Nginx 记录了两次连接后端的日志(因为设置了 proxy_next_upstream_tries 2)。这证明在返回降级页面之前,Nginx 已经尝试了自动恢复。
4. 恢复后端测试:
将 8080 端口的配置修改为 return 200 'Backend is recovered'; 并 reload。此时再次访问 80 端口,服务应立即恢复正常,无需重启防护层 Nginx。












易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图