nginx switches shutdown maintenance page with one click - the road to dream building

Background information

Operations such as downtime maintenance or system upgrades will affect user use. If the user does not see the shutdown maintenance notification and still accesses the system during the downtime maintenance, a default unfriendly access error interface will be prompted. At this time, if the user is under maintenance Directly displaying the specific information of the shutdown announcement will reduce user misunderstandings.

How to achieve

In Nginx, you can set a specific page as the page for "downtime maintenance" or "system upgrade". Assume that your downtime maintenance page is maintenance.html or maintenance.jpg, and it is located in /usr/share/nginx/html in the home directory of your website.

First, you need to find the server block where you need to configure the maintenance page in the Nginx configuration file, usually /etc/nginx/nginx.conf.

cat /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 {
        }
    }

The configuration means that if the /usr/share/nginx/html/maintenance.enable file exists, all requests will return 503 Service Unavailable and display the maintenance.jpg page.

When you need to perform system maintenance, you only need to create a maintenance.enable file in the home directory of the website. When the maintenance is completed, delete this file and normal access can be restored.

cat maintenance_start.sh 
#!/bin/bash
# 启动维护模式

# 创建维护标志文件
touch /usr/share/nginx/html/maintenance.enable
echo "Maintenance mode started."


cat  maintenance_stop.sh 
#!/bin/bash
# 停止维护模式 
# 删除维护标志文件
rm -rf  /usr/share/nginx/html/maintenance.enable
echo "Maintenance mode stoped."

 A relatively friendly handling method is provided here for reference only.

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/134866952
Recommended