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

时间:2016-11-17 17:21:38

标签: nginx webserver performance-testing

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

#include <stdio.h>

int main()
{

char characters;

printf("Input your characters: ");
scanf("%s", &characters);

FILE *fp = fopen("File.txt", "w");
fprintf(fp, "%s", characters);    
fclose(fp);

}

以下访问日志格式:

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'; 。我正在记录选择哪个上游服务器以及上游服务器的响应是什么。 Nginx docs

通过这样的配置,我想测试它可以处理的流量。为此,我使用了Apache Benchmark命令行工具,当时发送了许多concurent请求,如下所示:

[$upstream_addr - $upstream_response_time]

我的配置正在处理请求一段时间,直到它开始窒息(这是好的,我的应用程序有其限制)。但是当第一台服务器开始过载并且nginx必须开始使用备份服务器时,会发生一些奇怪的事情。 nginx不时尝试将请求传递给上游服务器,而不是上游名称!这当然不是作为主持人存在的。 ab -T application/json -H 'Authorization: Basic XXX' -p ab_post.json -n 1000 -c 30 https://example.com

请参阅日志片段:

... [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

我猜它是在所有上游服务器被标记为&#34;失败&#34;时发生的,但它只是一个猜测。我无法在文档或其他任何地方找到有关此类行为的任何内容。

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

Nginx上游enter link description here文档代表:

  

如果在与服务器通信期间发生错误,请求将被传递到下一个服务器,依此类推,直到将尝试所有正常运行的服务器。如果无法从任何服务器获得成功的响应,则客户端将收到与最后一个服务器的通信结果。

最糟糕的部分是即时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 响应。

一些环境信息:

502 Bad Gateway

Nginx从包含所有扩展程序nginx version: nginx/1.10.0 (Ubuntu) 包的默认包存储库中获取。

1 个答案:

答案 0 :(得分:1)

我和你有同样的问题,我在this website here找到答案。

  

如果是nginx,可以在$ upstream_addr变量中看到上游名称   无法选择要连接的上游服务器,因为全部   服务器按照max_fails / fail_timeout关闭。同时   错误日志中记录了“no live upstreams”错误。

我希望这可以帮到你。