簡體   English   中英

如何在根文件夾上進行Nginx故障轉移?

[英]How to do nginx failover on root folder?

我的nginx處理通過NFS掛載的/ www的請求。

我也有/ www2 ,它也是NFS,但是是從另一個(備份)服務器掛載的。 它具有與/ www相同的文件。

如果/ www損壞,如何使nginx自動將根目錄從/ www故障切換到/ www2

我嘗試設置“ 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 config不可能實現,但是您可以隨時編寫腳本。

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

確保更改路徑以匹配您的環境。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM