Search code examples
nginxhigh-availabilityblue-green-deploymentactive-passive

Nginx switches back to the first node after it comes back online


I am using Nginx as a failover cluster, where a service is given as an upstream. The service is running in both the nodes simultaneously and the Nginx is set up in third node which redirects to other nodes. When we switch off the service in one node it successfully switches to the backup node, but when the service in the 1st node is turned back on it switches back to the 1st node. Below is the relevant part of the configuration in Nginx:

http {
include       mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"'                
                  '"upstream: $upstream_addr"'
                  '"status: $upstream_status"';

access_log  logs\access.log  main;


sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

upstream backend {
    server ip1:port1 fail_timeout=5s max_fails=1;
    server ip2:port2 backup;
}

server {
    listen       8000;
    server_name  ip3;
    
    ssl_certificate      ...\certificate.crt;
    ssl_certificate_key  ...\privateKey.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    
location / {
        proxy_pass https://backend;
       
}

How can I stop switching the node to the primary node once the service is up again?

Thanks.


Solution

  • Write a bash code to change primary node to backup node once the primary node is backed up. Do let me know if you need the bash code