-1

When I send query to example.com/nodeService and node service on port 3001 returns 502 error, I need to redirect me on 3002 port without client application knowing about error. Is there such functionality in nginx? Thanks.

location /nodeService/ { #If this one is not available (502 error in my case)
                proxy_pass http://localhost:3001/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
            }

location /nodeService/ { # redirect me here!
                proxy_pass http://localhost:3002/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
            }

1 Answers1

0

By default, nginX will perform in-band (passive) health checking. If the response from a particular server fails with an error, nginX will mark this server as failed and will try to avoid selecting this server for a while.

The max_fails directive by default is 1, while fail_timeout by default is 10s. The servers will be tried in sequence until a healthy one is found. If none of them is healthy - nginX will return to the client the result from the last server.

http {
    upstream myapp1 {
        server http://localhost:3001/ max_fails=3 fail_timeout=5s;
        server http://localhost:3002/ max_fails=2 fail_timeout=6s;
    }

    server {
        listen 80;

        location /nodeService/ {
            proxy_pass http://myapp1;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}
IVO GELOV
  • 13,496
  • 1
  • 17
  • 26