nginx 配置 系统升级页面
默认80端口配置如下:
server {listen 80; # 指定端口号server_name 192.168.2.96; # 替换为实际域名或IP# 全局重定向到升级页面(排除自身防循环)if ($request_uri !~* "/upgrade.html") {return 307 /upgrade.html; # 临时重定向状态码}# 升级页面配置location = /upgrade.html {root D:/soft/nginx/nginx-1.23.2/html;add_header Retry-After 3600; # 建议重试时间(秒)}# 其他请求返回503(可选)location / {return 503; # 服务不可用状态码}
}
html页面代码
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>系统升级维护中</title><style>body {font-family: 'Microsoft YaHei', sans-serif;background-color: #f5f5f5;color: #333;text-align: center;padding: 0;margin: 0;height: 100vh;display: flex;justify-content: center;align-items: center;}.maintenance-container {background: white;padding: 40px;border-radius: 8px;box-shadow: 0 2px 10px rgba(0,0,0,0.1);max-width: 600px;}h1 {color: #e74c3c;margin-bottom: 20px;}.icon {font-size: 60px;margin-bottom: 20px;color: #f39c12;}.progress-bar {height: 6px;background: #ecf0f1;border-radius: 3px;margin: 30px 0;overflow: hidden;}.progress {height: 100%;width: 60%;background: #3498db;animation: progress 2s ease-in-out infinite;}@keyframes progress {0% { width: 30%; }50% { width: 70%; }100% { width: 30%; }}.contact {margin-top: 30px;font-size: 14px;color: #7f8c8d;}</style>
</head>
<body><div class="maintenance-container"><div class="icon">🔧</div><h1>系统升级维护中</h1><p>我们正在对系统进行升级维护,以提供更好的服务体验。</p><p>预计完成时间:2025年06月20日 18:00</p><div class="progress-bar"><div class="progress"></div></div><p>升级期间将暂时无法访问,给您带来不便敬请谅解。</p><div class="contact"><p>如有紧急问题,请联系:support@example.com</p></div></div>
</body>
</html>
如果端口号不是默认80,目前实现方式
比如:监听9980,端口号不是80, 重定向页面请求地址不带有端口号,目前通过折中的方式解决, 监听80进行代理:
server {listen 80;server_name 192.168.2.96;location / {proxy_pass http://127.0.0.1:9980; # 将请求转发到监听在9980端口的Nginx服务器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_set_header X-Forwarded-Proto $scheme;}
}