当端口仍然绑定但应用程序没有响应时,Nginx不将请求转发到备份上游

当端口仍然绑定但应用程序没有响应时,Nginx不将请求转发到备份上游,nginx,reverse-proxy,Nginx,Reverse Proxy,My nginx仅在主应用程序停止或根本不响应时将请求转发到备份上游应用程序。例如,当应用程序遇到“几乎内存不足”的情况时,它仍然会绑定端口,并且请求不会以某种方式重定向到备份应用程序,因为代理超时设置的缘故,请求不会被重定向到备份应用程序 我可以用kill-STOP复制场景: kill -STOP 34996 #PID of primary app telnet localhost 20401 Trying 127.0.0.1... Connected to localhost. #appl

My nginx仅在主应用程序停止或根本不响应时将请求转发到备份上游应用程序。例如,当应用程序遇到“几乎内存不足”的情况时,它仍然会绑定端口,并且请求不会以某种方式重定向到备份应用程序,因为代理超时设置的缘故,请求不会被重定向到备份应用程序

我可以用kill-STOP复制场景:

kill -STOP 34996 #PID of primary app
telnet localhost 20401 
Trying 127.0.0.1...
Connected to localhost. #application port still there
Escape character is '^]'.
浏览器请求只是超时,不会转发到备份应用程序实例

Nginx甚至会记录超时,但进一步的请求也会转到卡住的服务器,而不会转发到备份实例:

2016/08/22 14:16:03 [error] 104988#104988: *20813541 upstream timed out    (110: Connection timed out) while reading response header from upstream, client: 4.3.2.1, server: ~^(test\d*\.)?(example).(com), request: "HEAD /status-tocco HTTP/1.1", upstream: "http://127.0.0.1:16010/app-status", host: "myjavapp.example.com"
我本以为冻结主实例后的第一个请求应该在3s(由于代理连接超时)后失败,在接下来的500秒(失败超时)内,所有后续请求都应该转到备份实例,并且只有在其他请求到达原始服务器之后,我看对了吗

当我完全停止java进程时,所有请求都会立即重定向到备份服务器,不会出现任何问题

这是配置的相关部分:

upstream test {
    keepalive 10;
    server 127.0.0.1:20401 max_fails=1 fail_timeout=500s; #local, main instance
    server app06:16010 max_fails=1 fail_timeout=500s backup; #backup instance, different server
}

location / {
    proxy_pass http://test;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $http_host;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_connect_timeout 3s;
    proxy_send_timeout 3s;
    proxy_read_timeout 60s;       
    proxy_http_version 1.1; #keepalive
    proxy_set_header Connection ""; #keepalive

    add_header X-Cache $upstream_cache_status; # Request served by Cache?
    add_header X-AppServer $upstream_addr; # Backend Server / Port
    add_header X-AppServer-Status $upstream_status; # Backend HTTP Status
    add_header Strict-Transport-Security "max-age=15552000;" always;
}