nginx 是用php-fpm 来实现php解析的 也就是说 php升级 或者 php出问题的时候  nginx 是可以正常工作的,但是这个时候你如果访问index.php,会有一个冷冰冰的 502 bad gateway

这个太不友好了,如果在维护时候能够转向到一个静态页面就好了

 vhost 下建立2个 conf   default.conf 为正常的配置   offline.conf 为 转向维护提示页面的版本。 然后使用下面的脚本实现自动切换。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
PATH=/bin:/sbin:/wp-content/bin:/wp-content/sbin:/wp-content/local/bin:/wp-content/local/sbin:~/bin
export PATH

# Check if user is root

if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!n"
    exit 1
fi

printf "=========================================================================n"
printf "Offline switchern"
printf "=========================================================================n"
printf "Usage: /root/offline {offline|online|status}n"
printf "=========================================================================n"

NGINXDAEMON=/wp-content/local/nginx/sbin/nginx
PIDFILE=/wp-content/local/nginx/conf/vhost/offline.conf

function_offline()
{
    printf "offline ...n"
    if [ -f $PIDFILE ]; then
        printf "website is already offline!n"
        exit 1
    else
        mv /wp-content/local/nginx/conf/vhost/offline.conf.bak /wp-content/local/nginx/conf/vhost/offline.conf
        mv /wp-content/local/nginx/conf/vhost/default.conf /wp-content/local/nginx/conf/vhost/default.conf.bak
        $NGINXDAEMON -t
        $NGINXDAEMON -s reload
        printf "Website is successful offline!n"
    fi
}

function_online()
{
    printf "online...n"
    if [ -f $PIDFILE ]; then
        mv /wp-content/local/nginx/conf/vhost/offline.conf /wp-content/local/nginx/conf/vhost/offline.conf.bak
        mv /wp-content/local/nginx/conf/vhost/default.conf.bak /wp-content/local/nginx/conf/vhost/default.conf
        $NGINXDAEMON -t
        $NGINXDAEMON -s reload
        printf "Website is successful online!n"
    else
        printf "Website is already online!n"
    fi
    /etc/init.d/mysql stop
    /etc/init.d/php-fpm stop
}

function_status()
{
    if [ -f $PIDFILE ]; then
        printf "Website is offline!n"
    else
        printf "Website is online!n"
    fi
}

if [ "$1" = "offline" ]; then
    function_offline
elif [ "$1" = "online" ]; then
    function_online
elif [ "$1" = "status" ]; then
    function_status
else
    printf "Usage: /root/lnmp {offline|online|status}n"
fi