Nginx timeout configuration

Nginx timeout configuration

Nginx主要有四类超时设置:
客户端超时设置、
DNS解析超时设置、
代理超时设置,
如果使用ngx_lua,则还有lua相关的超时设置。

1. Client timeout settings
The main settings for client timeout include read request header timeout, read request body timeout, send response timeout, and long connection timeout. Use client timeout settings to avoid long-term connection occupancy caused by malicious clients or poor network conditions, affecting the server's processing capabilities.

client_header_timeout time:设置读取客户端请求头超时时间,默认为60s,
如果在此超时时间内客户端没有发送完请求头,
则响应408(RequestTime-out)状态码给客户端。
client_body_timeout time:设置读取客户端内容体超时时间,默认为60s,
此超时时间指的是两次成功读操作间隔时间,而不是发送整个请求体的超时时间,
如果在此超时时间内客户端没有发送任何请求体,
则响应408(RequestTime-out)状态码给客户端。
send_timeout time:设置发送响应到客户端的超时时间,默认为60s,
此超时时间指的也是两次成功写操作间隔时间,而不是发送整个响应的超时时间。
如果在此超时时间内客户端没有接收任何响应,则Nginx关闭此连接。
keepalive_timeout timeout [header_timeout]:设置HTTP长连接超时时间,
其中,第一个参数timeout是告诉Nginx长连接超时时间是多少,默认为75s。
第二个参数header_timeout是用于设置响应头“Keep-Alive: timeout=time”,
即告知客户端长连接超时时间。
两个参数可以不一样,“Keep-Alive:timeout=time”响应头可以在Mozilla和Konqueror系列浏览器起作用,
而MSIE长连接默认大约为60s,而不会使用“Keep-Alive: timeout=time”。
如Httpclient框架会使用“Keep-Alive: timeout=time”响应头的超时(如果不设置默认,则认为是永久)。
如果timeout设置为0,则表示禁用长连接。
**此参数要配合keepalive_disable 和keepalive_requests一起使用。**
keepalive_disable 表示禁用哪些浏览器的长连接,默认值为msie6,
即禁用一些老版本的MSIE的长连接支持。
keepalive_requests参数作用是一个客户端可以通过此长连接的请求次数,默认为100。

First, the browser will inform the server whether it supports long connections through the following request header when making a request.
Insert image description here
http/1.0 disables long connections by default, and you need to add the HTTP request header "Connection:Keep-Alive" to enable it. http/1.1 enables long connections by default and needs to be closed by adding the HTTP request header "Connection: close".

Next, if Nginx sets keepalive_timeout 5s, the browser will receive the following response header.
Insert image description here

2. DNS resolution timeout settings

resolver_timeout 30s: Set DNS resolution timeout, the default is 30s. It cooperates with resolver address ... [valid=time] to perform DNS domain name resolution. When using domain names in Nginx, you need to consider setting these two parameters. Use the following configuration in the community version of Nginx.

upstream backend {
    server c0.3.cn;
    server c1.3.cn;
}

The above two domain names will be parsed into IP addresses during the configuration file parsing stage of Nginx and recorded on upstream. When the IP addresses corresponding to these two domain names change, the upstream will not be updated. Nginx commercial version supports dynamic updates.

3. The proxy timeout setting
Nginx configuration is as shown below.

// backend_server定义了两个上游服务器192.168.61.1:9080(返回hello)
// 和192.168.61.1:9090(返回hello2)。
upstream backend_server {
 
    server 192.168.61.1:9080 max_fails=2 fail_timeout=10s weight=1;
 
    server 192.168.61.1:9090 max_fails=2 fail_timeout=10s weight=1;
 
}
 
server {
 
    location /test {
 
 		// 网络连接/读/写超时设置
       proxy_connect_timeout 5s;
       proxy_read_timeout 5s;
       proxy_send_timeout 5s;
 
 
 
 	 	// 失败重试机制设置、upstream存活超时设置。
       proxy_next_upstream error timeout;
       proxy_next_upstream_timeout 0;
       proxy_next_upstream_tries 0;
 
 		// backend_server定义了两个上游服务器192.168.61.1:9080(返回hello)和192.168.61.1:9090(返回hello2)。
       proxy_pass http://backend_server;
       add_header upstream_addr $upstream_addr;
 
    }
 
}

backend_server defines two upstream servers 192.168.61.1:9080 (returns hello) and 192.168.61.1:9090 (returns hello2).
The above instructions mainly have three sets of configurations: network connection/read/write timeout settings, failure retry mechanism settings, and upstream survival timeout settings.

Network connection/read/write timeout settings.

proxy_connect_timeout 60s:与后端/上游服务器建立连接的超时时间,默认为60s,此时间不超过75s。

proxy_read_timeout 60s:设置从后端/上游服务器读取响应的超时时间,默认为60s,此超时时间指的是两次成功读操作间隔时间,而不是读取整个响应体的超时时间,如果在此超时时间内上游服务器没有发送任何响应,则Nginx关闭此连接。

proxy_send_timeout 60s:设置往后端/上游服务器发送请求的超时时间,默认为60s,此超时时间指的是两次成功写操作间隔时间,而不是发送整个请求的超时时间,如果在此超时时间内上游服务器没有接收任何响应,则Nginx关闭此连接。

对于内网高并发服务,请根据需要调整这几个参数,比如内网服务TP999为1s,可以将连接超时设置为100~500毫秒,而读超时可以为1.5~3秒左右。

Failure retry mechanism settings.

proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |http_403 | http_404 | non_idempotent | off ...:
配置什么情况下需要请求下一台上游服务器进行重试。默认为“errortimeout”。
error表示与上游服务器建立连接、写请求或者读响应头出错。
timeout表示与上游服务器建立连接、写请求或者读响应头超时。
invalid_header表示上游服务器返回空的或错误的响应头。
http_XXX表示上游服务器返回特定的状态码。
non_idempotent表示RFC-2616定义的非幂等HTTP方法(POST、LOCK、PATCH),
也可以在失败后重试下一台上游服务器(即默认幂等方法GET、
HEAD、PUT、DELETE、OPTIONS、TRACE才可以重试)。
off表示禁用重试。

重试不能无限制进行,因此,需要如下两个指令控制重试次数和重试超时时间。

proxy_next_upstream_tries number:设置重试次数,默认0表示不限制,
注意此重试次数指的是所有请求次数(包括第一次和之后的重试次数之和)。

proxy_next_upstream_timeout time:设置重试最大超时时间,默认0表示不限制。

即在proxy_next_upstream_timeout时间内允许proxy_next_upstream_tries次重试。
如果超过了其中一个设置,则Nginx也会结束重试并返回客户端响应(可能是错误码)。

The following configuration indicates that the next upstream server in the upstream will be retried when error/timeout occurs. If the total retry time exceeds 6s or is retried once, it means that the retry failed (because it has been requested once before, so it is still can retry once), Nginx ends the retry and returns the client response.

   proxy_next_upstream error timeout;
 
   proxy_next_upstream_timeout 6s;
 
   proxy_next_upstream_tries 2;

4. upstream survival timeout setting

max_fails和fail_timeout:配置什么时候Nginx将上游服务器认定为不可用/不存活。
当上游服务器在fail_timeout时间内失败了max_fails次,则认为该上游服务器不可用/不存活。
并在接下来的fail_timeout时间内从upstream摘掉该节点(即请求不会转发到该上游服务器)。

什么情况下被认定为失败呢?其由 proxy_next_upstream定义,
不过,不管 proxy_next_upstream如何配置,
error, timeout and invalid_header 都将被认为是失败。

如server 192.168.61.1:9090max_fails=2 fail_timeout=10s;表示在10s内如果失败了2次,
则在接下来的10s内认定该节点不可用/不存活。
这种存活检测机制是只有当访问该上游服务器时,采取惰性检查,
可以使用ngx_http_upstream_check_module配置主动检查。

max_fails设置为0表示不检查服务器是否可用(即认为一直可用),
如果upstream中仅剩一台上游服务器时,则该服务器是不会被摘除的,
将从不被认为不可用。

5. ngx_lua timeout setting

When we use ngx_lua, please also consider setting network connection/read/write timeouts as follows.

lua_socket_connect_timeout  100ms;
 
lua_socket_send_timeout    200ms;
 
lua_socket_read_timeout    500ms;

When using lua, we will retry according to the following strategy.

if (status == 502 or status == 503 or status ==504) and request_time < 200 then
    resp =capture(proxy_uri)
    status =resp.status
    body =resp.body
    request_time = request_time + tonumber(var.request_time) * 1000
end

That is, if the status code is 500/502/503/504 and the request takes less than 200 milliseconds, we will retry.

Guess you like

Origin blog.csdn.net/weixin_44188105/article/details/129612184