如何对根文件夹执行nginx故障转移?

问题描述:

我的nginx处理来自/www的请求,该请求通过NFS挂载。如何对根文件夹执行nginx故障转移?

我也有/WWW2这也是NFS但安装从另一个(备份)服务器。它与/www具有相同的文件。

我怎样才能让nginx的自动/WWW故障转移的根目录/WWW2如果/WWW坏了?

我试着设置“root /”,然后是“try_files/www/ethaniel $ uri/www2/ethaniel $ uri”,但那不起作用。

这里是我的配置:

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

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

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

它无法通过nginx的配置,但你可以随时脚本它。

#!/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 

确保您更改路径以符合您的环境。