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

标签 nginx webserver performance-testing

您好,我有以下非常简单的负载均衡器/故障转移配置:

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

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

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

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.

希望这对您有帮助。

关于Nginx 尝试代理传递到上游名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40661246/

相关文章:

Nginx RTMP模块生成HLS

docker - 为docker-compose Web应用程序使用与localhost不同的主机名/URL

php - 我需要用 html 按钮(nginx)启动一个 php

php - 如何为不同的根目录配置 xampp Web 服务器

Javascript 性能 : How come looping through an array and checking every value is faster than indexOf, 搜索和匹配?

nginx - 如何使用 StrongLoop Nginx Controller 将 Nginx 设置为负载均衡器

linux - 使用 Specweb 2009 对 Apache 进行基准测试

nginx - 围攻未知 react

testing - LoadRunner 是否支持在响应页面中执行 JavaScript?

math - 为什么简化的数学方程比在 Julia-Lang 中有更多运算的等价方程运行得(稍微)慢?