yicr
12/29/2019 - 12:24 PM

Nginx : Tips / Simple maintenance script

# default
set $maintenance false;

# maintenance enable flag file
if (-e /var/tmp/do_maintenance) {
    set $maintenance true;
}
# except maintenace dir
if ($uri ~ "^/maintenance/") {
    set $maintenance false;
}
# except health check file
if ($uri = "/health_check.html") {
    set $maintenance false;
}
# except remote ip 
if ($remote_addr = 124.35.147.218) {
    set $maintenance false;
}
# except forwarded ip
if ($http_x_forwarded_for = "124.35.147.218") {
    set $maintenance false;
}
error_page 503 /maintenance/maintenance.html;
location /maintenance/ {
    root /data/var/www/html;
}
if ($maintenance = true) {
    return 503;
}

start maintenance mode

$ touch /var/tmp/do_maintenance

end maintenace mode

$ rm /var/tmp/do_maintenance