apache-2.2 – Nginx作为反向代理:如何正确配置网关超时?

前端之家收集整理的这篇文章主要介绍了apache-2.2 – Nginx作为反向代理:如何正确配置网关超时?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们已将Nginx配置为Apache服务器场的反向代理,但我遇到了网关超时问题.

我们以人类可读的形式实现的目标是:“在一秒钟内发送请求,但如果确实需要更长时间,则无论如何都要提供”,这对我来说意味着“在上游尝试第一台Apache服务器最长500毫秒.如果我们得到超时/一个错误,尝试下一个,依此类推,直到我们最终成功.“

现在我们的相关配置如下:

location @proxy {
    proxy_pass         http://apache$request_uri;

    proxy_connect_timeout 1s;
    proxy_read_timeout 2s;

}

[...]

upstream apache {
 server 127.0.0.1:8001          max_fails=1 fail_timeout=10s;
 server 10.1.x.x:8001           max_fails=1 fail_timeout=10s backup;
 server 10.1.x.x:8001           max_fails=1 fail_timeout=10s backup;
 server 10.1.x.x:8001           max_fails=1 fail_timeout=10s backup;
}

这里的问题是Nginx似乎误解为“尝试在一秒内从整个上游集群获得响应,如果我们不这样做就会发出50X错误 – 没有任何限制尝试任何上游服务器的时间”,显然不是我们想到的.

有没有办法让Nginx做我们想要的?

最佳答案
我想你需要的是:

max_fails = 0

proxy_next_upstream =超时

根据文件

max_fails=number

sets the number of unsuccessful attempts to
communicate with the server that should happen in the duration set by
the fail_timeout parameter to consider the server unavailable for a
duration also set by the fail_timeout parameter. By default,the
number of unsuccessful attempts is set to 1. The zero value disables
the accounting of attempts. What is considered an unsuccessful attempt
is defined by the proxy_next_upstream,fastcgi_next_upstream,
uwsgi_next_upstream,scgi_next_upstream,and memcached_next_upstream
directives.

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

和:

Syntax: proxy_next_upstream error | timeout | invalid_header |
http_500 | http_502 | http_503 | http_504 | http_403 | http_404 |
non_idempotent | off …; Default: proxy_next_upstream error timeout;

Context: http,server,location Specifies in which cases a request
should be passed to the next server:

error

an error occurred while establishing a connection with the
server,passing a request to it,or reading the response header;

timeout

a timeout has occurred while establishing a connection with
the server,or reading the response header;

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream

原文链接:https://www.f2er.com/nginx/435631.html

猜你在找的Nginx相关文章