我的nginx仅在主应用程序停止时才将请求转发到备份上游应用程序,或者根本不响应。例如,当应用程序正在与“近乎内存不足”的情况作斗争时,它仍会绑定端口并以某种方式请求不会被重定向到备份应用程序,因为它们应该因为proxy_timeout设置而应该。
我能用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之后失败(由于tue proxy_connect_timeout ),在接下来的500秒内( fail_timeout )所有后续请求应该转到备份实例,只有在其他请求到达原始服务器之后,我才能看到这个吗?
当我完全停止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;
}