2017-02-28 1 views
0

Mein nginx verarbeitet Anfragen von /www die über NFS gemountet ist.Wie Nginx Failover auf Stammordner zu tun?

Ich habe auch /www2, die auch NFS ist aber von einem anderen (Backup-) Server bereitgestellt. Es hat dieselben Dateien wie /www.

Wie kann ich nginx automatisch das Verzeichnis root Failover von /www zu /www2 wenn /www gebrochen ist?

Ich habe versucht, "root /" und dann "try_files/www/ethaniel $ uri/www2/ethaniel $ uri", aber das hat nicht funktioniert.

Hier ist meine config:

server { 
    listen 80; 
    server_name .ethaniel.com; 
    root /www/ethaniel; 

    location/{ 
     index index.php; 
     autoindex off; 
    } 

    location ~* ^.+\.(php)$ { 
     include /etc/nginx/fastcgi.conf.include; 
    } 
} 

Antwort

0

Es ist nicht möglich, über nginx Config, aber man kann immer Skript es.

#!/bin/sh 

#check nfs mount status 
df -P -T /storage-pool/nfs-1 | tail -n +2 | awk '{print $2}' | grep nfs | wc -l > /root/status/nfs-1 
df -P -T /storage-pool/nfs-2 | tail -n +2 | awk '{print $2}' | grep nfs | wc -l > /root/status/nfs-2 

#give time to fetch nfs content 
sleep 5 

CHECK_FILE_1=`cat /root/status/nfs-1` 
CHECK_FILE_2=`cat /root/status/nfs-2` 

CHECK_NGINX_1=`grep 'nfs-1' /etc/nginx/sites-enabled/default.conf | wc -l` 
CHECK_NGINX_2=`grep 'nfs-2' /etc/nginx/sites-enabled/default.conf | wc -l` 

if [ $CHECK_FILE_1 -ne 1 ] && [ $CHECK_NGINX_1 -eq 1 ] && [ $CHECK_FILE_2 -eq 1 ] 
    then 
    sed -i 's/nfs-1/nfs-2/g' /etc/nginx/sites-enabled/default.conf 
     nginx -t && echo 'move to nfs-2' 
     nginx -s reload 

elif [ $CHECK_FILE_2 -ne 1 ] && [ $CHECK_NGINX_2 -eq 1 ] && [ $CHECK_FILE_1 -eq 1 ] 
    then 
    sed -i 's/nfs-2/nfs-1/g' /etc/nginx/sites-enabled/default.conf 
     nginx -t && echo 'move to nfs-1' 
     nginx -s reload 

elif [ $CHECK_FILE_1 -ne 1 ] && [ $CHECK_FILE_2 -ne 1 ] 
     then 
     echo 'All nfs-server down' 

else 
    echo 'OK' 

fi 

Stellen Sie sicher, dass Sie die Pfade entsprechend Ihrer Umgebung ändern.