Nginx 尝试代理传递给上游名称

Nginx tries to proxy pass to upstream name

您好,我有以下非常简单的负载配置 balancer/fail:

upstream backend_stream {
    server 192.168.0.130:8080   max_fails=2 fail_timeout=30s;
    server 192.168.0.131:8080   max_fails=2 fail_timeout=30s backup;
}

server {
    listen 443 ssl;
    server_name exmaple.com;

    # SSL stuff

    proxy_ignore_client_abort on;
    proxy_connect_timeout 3s;
    proxy_read_timeout 5s;
    proxy_send_timeout 5s;
    send_timeout 20s;
    proxy_next_upstream_timeout 60s;
    proxy_next_upstream_tries 0;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 non_idempotent;

    proxy_pass  http://backend_stream;

    access_log /path/to/access.log vhosts_extra;
    error_log /path/to/error.log;
}

以及以下访问日志格式:

log_format vhosts_extra '$host:$server_port $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" [$upstream_addr - $upstream_response_time] $request_time';

最重要的是最后的部分:[$upstream_addr - $upstream_response_time]。我正在记录选择了哪个上游服务器以及上游服务器的响应是什么。 Nginx docs.

有了这样的配置,我想测试一下它能处理什么流量。为此,我当时使用 Apache Benchmark 命令行工具发送了多个并发请求,如下所示:

ab -T application/json -H 'Authorization: Basic XXX' -p ab_post.json -n 1000 -c 30 https://example.com

我的配置会处理请求一段时间,直到它开始阻塞(没关系,我的应用程序有其限制)。但是当第一台服务器开始超载并且 nginx 必须开始使用备份服务器时,一些奇怪的事情发生了。有时 nginx 试图不将请求传递给上游服务器,而是传递给上游名称!这当然不作为主机存在。 ... [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.051

查看日志片段:

example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:54 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 3.730] 8.783
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:54 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 3.843] 3.907
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:54 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 3.942] 9.003
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:54 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 4.044] 4.106
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:55 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 4.125] 9.182
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:55 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 4.249] 4.309
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:55 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 4.306] 9.372
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:55 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 4.410] 4.477
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:55 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.001, 4.504] 9.566
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:56 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 4.583] 4.642
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:56 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 4.707] 9.770
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:56 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.131:8080 - 4.798] 4.860
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:56 +0000] "POST / HTTP/1.0" 200 1183 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 4.884] 9.945
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:56 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.048
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:57 +0000] "POST / HTTP/1.0" 504 176 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.000, 5.002] 10.061
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:57 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.051
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:57 +0000] "POST / HTTP/1.0" 504 176 "-" "ApacheBench/2.3" [192.168.0.130:8080, 192.168.0.131:8080 - 5.001, 5.000] 10.059
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:57 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.052

有时候真的很努力...

example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:58 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.002, 0.000] 5.050
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:58 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.048
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:58 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.064
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:58 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.065
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:59 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.059
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:59 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.062
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:59 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.064
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:59 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.063
example.com:443 12.34.12.34 - - [17/Nov/2016:16:15:59 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.066
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:00 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.051
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:00 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.055
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:00 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.066
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:00 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.057
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:00 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.000, 0.000] 5.032
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:01 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.001, 0.000] 5.001
example.com:443 12.34.12.34 - - [17/Nov/2016:16:16:01 +0000] "POST / HTTP/1.0" 502 166 "-" "ApacheBench/2.3" [192.168.0.131:8080, backend_stream - 5.002, 0.000] 5.002

我猜想当所有上游服务器都标记为 "failing" 时会发生这种情况,但这只是一个猜测。我在文档或其他任何地方都找不到有关此类行为的任何信息。

这是正常行为吗?我应该知道它可能会发生,还是我做错了什么?

Nginx 上游enter link description here文档站:

If an error occurs during communication with a server, the request will be passed to the next server, and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will receive the result of the communication with the last server.

最糟糕的部分是即时 502 Bad Gateway 响应。

一些环境信息:

nginx version: nginx/1.10.0 (Ubuntu)

Nginx 从默认包存储库安装所有扩展,nginx-extras 包。

我遇到了和你一样的问题,我在this website here上找到了答案。

Upstream name can be seen in the $upstream_addr variable if nginx wasn't able to select an upstream server to connect to because all servers were down as per max_fails/fail_timeout. At the same time the "no live upstreams" error is logged to the error log.

希望对您有所帮助。