如果第一个地址不可用,则将Proxy_pass传递到第二个地址(例如502)

当我向example.com/nodeService发送查询并且端口3001上的节点服务返回502错误时,我需要在3002端口上重定向我,而客户端应用程序不知道错误。 Nginx中有这种功能吗?谢谢。

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;
            }
sodesune8899 回答:如果第一个地址不可用,则将Proxy_pass传递到第二个地址(例如502)

默认情况下,nginX将执行带内(被动)运行状况检查。如果来自特定服务器的响应失败并出现错误,nginX会将其标记为失败服务器,并尝试避免一段时间选择该服务器。

max_fails伪指令默认为1,而fail_timeout伪指令为10s。将依次尝试服务器,直到找到正常的服务器为止。如果它们都不健康,nginX将从最后一台服务器返回的结果返回给客户端。

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;
        }
    }
}
本文链接:https://www.f2er.com/3097373.html

大家都在问