评论

Nginx之upstream被动式重试机制解读

原标题:Nginx之upstream被动式重试机制解读

基本介绍

我们使用Nginx通过反向代理做负载均衡时,如果被代理的其中一个服务发生错误或者超时的时候,通常希望Nginx自动重试其他的服务,从而实现服务的高可用性。实际上Nginx本身默认会有错误重试机制,并且可以通过proxy_next_upstream来自定义配置。

Nginx 通过proxy_next_upstream参数来定义什么情况下会被认为是 fails,从而触发失败重试机制。

fails 可以分成两类:

  1. 默认错误,包括 error、timeout
  2. 选择定义错误,包含 invalid_header 以及各种异常 http 状态码错误等
默认错误

出现error的场景,常见的是上游服务器的服务重启、停止,或者异常崩溃导致的无法提供正常服务。而timeout的情况,就是代理请求过程中达到对应的超时配置,主要包括了:

  • proxy_connect_timeout,建立三次握手的时间
  • proxy_read_timeout,建立连接后,等待上游服务器响应以及处理请求的时间
  • proxy_send_timeout,数据回传的间隔时间(注意不是数据发送耗时)
选择定义错误

异常状态码部分(就是 4xx、5xx 错误)。上游服务器返回空响应或者非法响应头

invalid_header: a server returned an empty or invalid response;

其默认值是proxy_next_upstream error timeout,即发生网络错误以及超时,才会重试其他服务器。默认情况下服务返回500状态码是不会重试的

指令配置 proxy_next_upstream

设置当连接upstream服务器集群中的某个服务器第一次失败时,指定在哪些情况下将请求传递到下一个服务器

语法: proxy_next_upstream error |timeout |invalid_header |http_500 |http_502 |http_503 |http_504 |http_403 |http_404 |http_429 |non_idempotent |off ...;

默认: proxy_next_upstream error timeout;

使用位置: http,,serverlocation

  • error # 与服务器建立连接,向其传递请求或读取响应头时发生错误;
  • timeout # 在与服务器建立连接,向其传递请求或读取响应头时发生超时;
  • invalid_header # 服务器返回空的或无效的响应;
  • http_500 # 服务器返回代码为500的响应;
  • http_502 # 服务器返回代码为502的响应;
  • http_503 # 服务器返回代码为503的响应;
  • http_504 # 服务器返回代码504的响应;
  • http_403 # 服务器返回代码为403的响应;
  • http_404 # 服务器返回代码为404的响应;
  • http_429 # 服务器返回代码为429的响应(1.11.13);
  • non_idempotent # 通常,请求与 非幂等 方法(POST,LOCK,PATCH)不传递到请求是否已被发送到上游服务器(1.9.13)的下一个服务器; 启用此选项显式允许重试此类请求;
  • off # 禁用将请求传递给下一个服务器。

当请求类型是POST时,Nginx默认不会失败重试如果想让POST请求也会失败重试,需要配置non_idempotent。

配置示例:

upstream nginxretry {

server 127.0.0.1:9030weight=10;

server 127.0.0.1:9031weight=10;

}

server {

listen 9039;

location /{

proxy_pass http://nginxretry;

proxy_next_upstream error timeout http_500;

}

}

proxy_next_upstream_timeout

设置重试的超时时间,超时后不再重试,给用户返回错误,默认为0,即不做限制

语法: proxy_next_upstream_timeout time;
Default: proxy_next_upstream_timeout 0;
Context: http,server,location
proxy_next_upstream_tries

设置重试的最大次数,若超过重试次数,也不再重试,默认为0,即不做限制(proxy_next_upstream_timeout时间内允许proxy_next_upstream_tries次重试,包括第一次)

语法: proxy_next_upstream_tries number;
Default: proxy_next_upstream_tries 0;
Context: http,server,location

配置示例:

server {

proxy_next_upstream error timeout;

proxy_next_upstream_timeout 15s;

proxy_next_upstream_tries 5;

}

重试限制方式

默认配置是没有做重试机制进行限制的,也就是会尽可能去重试直至失败。

Nginx 提供了以下两个参数来控制重试次数以及重试超时时间:

  • proxy_next_upstream_tries:设置重试次数,默认0表示无限制,该参数包含所有请求 upstream server 的次数,包括第一次后之后所有重试之和;
  • proxy_next_upstream_timeout:设置重试最大超时时间,默认0表示不限制,该参数指的是第一次连接时间加上后续重试连接时间,不包含连接上节点之后的处理时间

对upstream中某单一服务器的限制

  • max_fails:最大失败次数(0为标记一直可用,不检查健康状态)
  • fail_timeout:失败时间(当fail_timeout时间内失败了max_fails次,标记服务不可用fail_timeout时间后会再次激活次服务)

配置示例1:

upstream httpget {

server 192.168.111.101:8080max_fails=5fail_timeout=10s;

server 192.168.111.102:8080;

}

配置示例2:

proxy_connect_timeout 3s;

proxy_next_upstream_timeout 6s;

proxy_next_upstream_tries 3;

upstream test {

server 127.0.0.1:8001fail_timeout=60s max_fails=2;# Server A

server 127.0.0.1:8002fail_timeout=60s max_fails=2;# Server B

server 127.0.0.1:8003fail_timeout=60s max_fails=2;# Server C

}返回搜狐,查看更多

责任编辑:

平台声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
阅读 ()
大家都在看
推荐阅读