High Availability Using Nginx Failover Mechanism

Hi I am trying to setup a reverse proxy failover between two different servers
One as main (server1), the other as a backup(server2)
The idea is that if the main server is not available it should switch to the backup

So i have edited the nginx.conf in the frappe-bench/config folder

Upstream Directive

upstream frappe-bench-frappe {
    server 127.0.0.1:8000 fail_timeout=0;
    server 11.22.33.44 backup ;

}

upstream frappe-bench-socketio-server {
	server 127.0.0.1:9000 fail_timeout=0;
    server 11.22.33.44 backup ;

}

Server Blocks

#server1
server {
	listen 443;
	server_name
		server1.company.com
		;

	root /home/ubuntu/frappe-bench/sites;

	

	
	ssl on;
	ssl_certificate      /etc/letsencrypt/live/server1.company.com/fullchain.pem;
	ssl_certificate_key  /etc/letsencrypt/live/server1.company.com/privkey.pem;
	ssl_session_timeout  5m;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers ciphers
	ssl_prefer_server_ciphers   on;
	

	add_header X-Frame-Options "SAMEORIGIN";

    location /socket.io {
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header X-Frappe-Site-Name $host;
		proxy_set_header Origin $scheme://$http_host;
		proxy_set_header Host $host;

		proxy_pass http://frappe-bench-socketio-server;
	}
    location @webserver {
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header X-Frappe-Site-Name $host;
		proxy_set_header Host $host;
		proxy_set_header X-Use-X-Accel-Redirect True;
		#proxy_read_timeout 7200;
		proxy_redirect off;
		proxy_connect_timeout  10s;
            proxy_read_timeout     10s;
            proxy_next_upstream    error timeout invalid_header http_500 http_504 http_503;
		proxy_pass  http://frappe-bench-frappe;
	}
 #http to https redirect
	server {
		listen 80;
		server_name
			server1.company.com
			;

		return 301 https://$host$request_uri;
	}

#server 2
    server {
        listen 443;
        server_name server2.company.com;
	    ssl on;
        ssl_certificate      /path_to/server2.company.com/fullchain.pem;
        ssl_certificate_key  /path_to/server2.company.com/privkey.pem;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	
	    add_header X-Frame-Options "SAMEORIGIN";

	    location /  {
		    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Frappe-Site-Name $host;
                proxy_set_header Host $host;
                proxy_set_header X-Use-X-Accel-Redirect True;
                proxy_pass http://frappe-bench-frappe;
	}
}

 server {
                listen 80;
                server_name
                        server2.company.com
                        ;

                return 301 https://$host$request_uri;
        }

so if i stop the bench on server1 which will make the hostname server1.company.com,
it should switch to server2.company.com,
but this doesnt happen, perhaps my configuration is not correct?
Any insights or help would be appreciated

@oderao
Really appreciate your efforts.

Here in this scenario, how would you manage the same database to your backup server even though you have entered $host in Server 2 block if the main server is down

The DB’s of the main and backup server will have a master/slave relationship,
I’m not sure i understand your reference to the $host variable