skip to Main Content

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  logsaccess.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.

2

Answers


  1. This is the desired behaviour. If NGINX is balancing between two nodes and one of them is marked as backup NGINX will just switch to this note in case the primary node is done.

    As soon as the primary node is back up and healthy, NGINX will always switch back to the primary node.

    Read more about it here. https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

    If you are looking for something like Blue / Green deployments there are other concepts to do it.

    One that works with NGINX Open Source is available here.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search