Nginx配置维护模式

一、Nginx配置文件

java 复制代码
 server {
    listen 80;
    server_name yourdomain.com;

    # 核心逻辑:检测维护标记文件是否存在
    if (-f /var/www/maintenance.enable) {
        # 如果文件存在,返回 509 状态码,并重写为维护页面
        return 509;
    }

    # 错误页面重定向
    error_page 509 @maintenance;
    location @maintenance {
        root /usr/share/nginx/html;
        rewrite ^(.*)$ /maintenance.html break;
    }

    # 正常的业务路由
    location / {
        proxy_pass http://backend_server;
    }
}

二、HTML文件(maintenance.html)

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>
        /* 基础重置与全局样式 */
        * { 
			margin: 0; 
			padding: 0; 
			box-sizing: border-box; 
		}
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #333;
            text-align: center;
            padding: 20px;
        }

        /* 内容卡片样式 */
        .container {
            background: #ffffff;
            padding: 50px 40px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            max-width: 600px;
            width: 100%;
        }

        /* 图标与标题 */
        .icon { 
			font-size: 60px; 
			margin-bottom: 20px; 
		}
		
        h1 { 
			font-size: 28px; 
			margin-bottom: 15px; 
			color: #2c3e50; 
		}
		
        p { 
			font-size: 16px; 
			line-height: 1.6; 
			color: #555; 
			margin-bottom: 10px; 
		}

        /* 移动端适配 */
        @media (max-width: 480px) {
            .container { padding: 30px 20px; }
            h1 { font-size: 22px; }
            .countdown { font-size: 18px; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="icon">🛠️</div>
        <h1>系统正在维护升级</h1>
        <p>系统维护升级期间,网站将暂时无法访问</p>
    </div>
</body>
</html>

三、命令

bash 复制代码
# 检查配置文件语法
nginx -t

# 重载配置
nginx -s reload

# 开启维护模式,拦截用户请求
touch /var/www/maintenance.enable


# 关闭维护模式,恢复用户访问
rm -f /var/www/maintenance.enable
相关推荐
Elastic 中国社区官方博客36 分钟前
Elastic 在 Everest Group 企业搜索产品 PEAK Matrix® 评估 2026 中被评为领导者
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
程序员在囧途36 分钟前
likeadmin-api API 中转站怎么做统一报价?从 /pricing、/user/balance 到 task_id 回执的落地方法
运维·服务器·数据库·likeadmin-api·api中转站·token计费
想你依然心痛2 小时前
Linux用户空间与内核空间通信:netlink、ioctl、mmap 零拷贝与性能对比
linux·运维·服务器
沉静的小伙2 小时前
在微服务中使用领域事件
java·运维·微服务
wenzhangli72 小时前
oodAgent 4.0 Skills分布式调度 — 从Code Agent实战看自主维护的Agent网络
运维·网络·人工智能·自动化
做个文艺程序员3 小时前
Linux第12篇:性能监控与瓶颈分析——CPU/内存/IO/网络全维度
linux·运维·网络
K姐研究社3 小时前
OiiOii 2.0 实测 – 智能画布实现 AI 视频创作自动化
运维·人工智能·自动化
HAPPY酷3 小时前
【ROS2】 Conda 环境搭建(命令行版)
linux·运维·python·ubuntu·机器人·conda
wang_shu_mo_ran4 小时前
网络编程(一):网络编程初识
运维·服务器·网络
姚青&5 小时前
持续交付与 DevOps 体系
运维·devops