当我向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;
}
答案 0 :(得分:0)
默认情况下,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;
}
}
}