nginx+shell脚本实现一键启用与关闭停机维护页面

简介: nginx+shell脚本实现一键启用与关闭停机维护页面

需求:

由于需要进行停机维护或者系统升级等操作,会影响到用户使用

如果停机维护期间用户未看到停机维护的通知,仍去访问系统,会提示默认不太友好的访问错误界面

这时如果在维护的时候直接展示停机公告的具体信息,会减少用户的误解

(图片点击放大查看)

下面介绍nginx下实现一键脚本启用和关闭停机维护页面

在 Nginx 中,可以设置一个特定的页面来作为"停机维护"或"系统升级"的页面。假设你的停机维护页面为maintenance.html或者maintenance.jpg,并且它位于你的网站主目录的/usr/share/nginx/html中。

首先,你需要在 Nginx 的配置文件中,通常是/etc/nginx/nginx.conf,找到需要配置维护页面的 server 块。

vim  /etc/nginx/nginx.conf然后添加或修改如下配置:

server {
        listen       80;
        listen       [::]:80;
        server_name  _;
       # root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        if (-f $document_root/maintenance.enable) {
        return 503;
        }
        location / {
             root /usr/share/nginx/html;
             index index.html index.htm;
             try_files $uri $uri/ =404;
        }
        # 定义503错误页面
        error_page 503 @maintenance;
        location @maintenance {
              root /usr/share/nginx/html;
             rewrite ^(.*)$ /maintenance.jpg break;
        }
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502  504 /50x.html;
        location = /50x.html {
        }
    }

然后重载nginx

systemctl reload nginx

(图片点击放大查看)

配置的意思是,如果存在/usr/share/nginx/html/maintenance.enable文件,则所有请求都返回503服务不可用,并显示maintenance.jpg页面。

当你需要进行系统维护时,只需要在网站主目录下创建一个maintenance.enable文件即可 当维护结束,删除这个文件,即可恢复正常访问:

然后创建两个shell脚本,即可一键脚本实现

[root@localhost ~]# cd /opt/
[root@localhost opt]# ll
total 8
-rwxr-xr-x 1 root root 141 Dec  2 22:40 maintenance_start.sh
-rwxr-xr-x 1 root root 142 Dec  2 22:40 maintenance_stop.sh
[root@localhost opt]# cat maintenance_start.sh 
#!/bin/bash
# 启动维护模式
# 创建维护标志文件
touch /usr/share/nginx/html/maintenance.enable
echo "Maintenance mode started."
[root@localhost opt]# 
[root@localhost opt]# 
[root@localhost opt]# cat  maintenance_stop.sh 
#!/bin/bash
# 停止维护模式 
# 删除维护标志文件
rm -rf  /usr/share/nginx/html/maintenance.enable
echo "Maintenance mode stoped."

效果如下

1、一键脚本启动维护模式,展示停机维护界面

/opt/maintenance_start.sh


(图片点击放大查看)

2、一键脚本停止维护模式

/opt/maintenance_stop.sh


相关文章
|
4天前
|
监控 Unix Shell
shell脚本编程学习
shell脚本编程
22 12
|
8天前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
11天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
27 2
|
1月前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
43 17
|
1月前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
36 12
|
1月前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
36 8
|
1月前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
2月前
|
运维 监控 Shell
自动化运维之宝:编写高效的Shell脚本
【8月更文挑战第31天】在运维的世界里,Shell脚本是一把瑞士军刀,它让日常任务变得简单而高效。本文将通过浅显易懂的语言和实际案例,带你领略Shell脚本的魅力,并教你如何打造属于自己的自动化工具箱。无论你是初学者还是资深运维,这篇文章都将为你打开一扇窗,让你看到不一样的风景。让我们一起探索Shell脚本的世界吧!
|
2月前
|
存储 Shell 数据安全/隐私保护
minio一键安装脚本分享(shell和python)
minio一键安装脚本分享(shell和python)
46 0
|
2月前
|
关系型数据库 Shell 数据库
postgres14一键安装脚本分享(shell和python)
postgres14一键安装脚本分享(shell和python)
36 0
下一篇
无影云桌面