Nginx的超时timeout配置详解

本文介绍 Nginx 的 超时(timeout)配置。分享给大家,具体如下:

Nginx 处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提升 Nginx 的性能。

keepalive_timeout

HTTP 是一种无状态协议,客户端向服务器发送一个 TCP 请求,服务端响应完毕后断开连接。

如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。

HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

Nginx 使用 keepalive_timeout 来指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。

# 配置段: http, server, location
keepalive_timeout 60s;

client_body_timeout

指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)。

# 配置段: http, server, location
client_body_timeout 20s;

client_header_timeout

客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。

# 配置段: http, server, location
client_header_timeout 10s;

send_timeout

服务端向客户端传输数据的超时时间。

# 配置段 : http, server, location
send _ timeout 30 s;

客户度连接nginx超时, 建议5s内

接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408

Syntax: client_header_timeout time;
Default:
client_header_timeout 60s;
Context: http, server
Defines a timeout for reading client request header. If a client does not transmit the entire header within this time,
the 408 (Request Time-out) error is returned to the client.

接收客户端body超时, 默认60s, 如果连续的60s内没有收到客户端的1个字节, 返回408

Syntax: client_body_timeout time;
Default:
client_body_timeout 60s;
Context: http, server, location
Defines a timeout for reading client request body. The timeout is set only for a period between two successive read operations, not for the transmission of the whole request body.
If a client does not transmit anything within this time,
the 408 (Request Time-out) error is returned to the client.

keepalive时间,默认75s,通常keepalive_timeout应该比client_body_timeout大

Syntax: keepalive_timeout timeout [header_timeout];
Default:
keepalive_timeout 75s;
Context: http, server, location
The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections.
The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.

The “Keep-Alive: timeout=time” header field is recognized by Mozilla and Konqueror. MSIE closes keep-alive connections by itself in about 60 seconds.

可以理解为TCP连接关闭时的SO_LINGER延时设置,默认5s

Syntax: lingering_timeout time;
Default:
lingering_timeout 5s;
Context: http, server, location
When lingering_close is in effect, this directive specifies the maximum waiting time for more client data to arrive. If data are not received during this time,
the connection is closed. Otherwise, the data are read and ignored, and nginx starts waiting for more data again.
The “wait-read-ignore” cycle is repeated, but no longer than specified by the lingering_time directive.

域名解析超时,默认30s

Syntax: resolver_timeout time;
Default:
resolver_timeout 30s;
Context: http, server, location
Sets a timeout for name resolution, for example:
resolver_timeout 5s;

发送数据至客户端超时, 默认60s, 如果连续的60s内客户端没有收到1个字节, 连接关闭

Syntax: send_timeout time;
Default:
send_timeout 60s;
Context: http, server, location
Sets a timeout for transmitting a response to the client. The timeout is set only between two successive write operations,
not for the transmission of the whole response. If the client does not receive anything within this time, the connection is closed.

nginx与upstream server的连接超时时间

Syntax: proxy_connect_timeout time;
Default:
proxy_connect_timeout 60s;
Context: http, server, location
Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭

Syntax: proxy_read_timeout time;
Default:
proxy_read_timeout 60s;
Context: http, server, location
Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations,
not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.

nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭

Syntax: proxy_send_timeout time;
Default:
proxy_send_timeout 60s;
Context: http, server, location
Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations,
not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.

作者:架构师之路
链接:https://juejin.im/post/5b696a24e51d45191e0d3e56
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

排查 Chrome 网络问题

有时用户请求接口或服务不一定是服务器的问题,也及有可能是用户网络问题。如果用户使用chrome,可以很方便的用chrome自带的网络请求分析工具:

1.在Chrome新开一个标签输入chrome://net-export/;
2.点击start logging disk,存储文件
3.切换到原页面,进行正常操作,操作完成之后,回到刚才的chrome://net-export/,点击stop logging

点击 netlog_viewer.  即 https://chromium.googlesource.com/catapult/+/master/netlog_viewer/

点击 选择文件

https://netlog-viewer.appspot.com/

选择刚才生成的json文件导入即可。

具体的选项和解释如下:

选项 您可以执行的操作
Capture(捕获)

选择如何捕获数据。

  • 选择 Discard old data under memory pressure(在内存不足时舍弃旧数据),避免因捕获数据时间过长而出现崩溃情况。
  • 选择 Include the actual bytes sent/received(包括发送/接收的实际字节),将此信息添加到日志中。如果您选择此选项,可能会导致日志文件过大,还可能会导致敏感数据泄露。

您随时可以 Stop(停止)或 Reset(重置)捕获设置。

Export(导出) 此选项从 Chrome 58 起就已弃用。请改为使用 chrome://net-export/
Import(导入) 将导出的 .json 格式的 net-internals 文件导入。然后,您就可以查看有关网络事件的信息了。
Proxy(代理) 查看浏览器所使用的代理设置的相关信息。如果没有使用代理,您就会看到 Use Direct connections(使用直接连接)。
Events(事件) 即时查看事件列表。事件包含套接字连接、SPDY 会话、HTTP-TCP 连接和网址请求。错误消息会以红色文字显示。
Timeline(时间轴) 查看包含信息的图表,例如打开或使用中的套接字数量、网址和 DNS 请求数量,或发送/接收的数据量。
DNS 查看设备的 DNS 查询日志。如果网页加载失败,此选项有助于排查相关问题。日志中会列出相应网址及其对应的 IP,还会包含 DNS 请求的时间。
Sockets(套接字) 查看打开和已使用的套接字的日志。您可以使用此日志排查高级网络问题。
Alt-Svc 查看与替代服务映射有关的信息。
HTTP/2 查看 HTTP/2 会话日志和替代服务映射。
QUIC 查看有关快速 UDP 互联网连接 (QUIC) 的信息。这是一种实验性网络协议,可优化依赖于 TCP 并以连接为目的的网络应用。您可以前往 chrome://flags/#enable-quic,启用或停用 QUIC。
SDCH 查看有关“面向 HTTP 的共享字典压缩”(SDCH) 的信息。这是一种数据压缩算法,会在编码或解码之前,先使用预先协议的字典调整内部状态。字典可能是事先存储在本地的,也可能是从其他地方上传或缓存的。
Cache(缓存) 查看已缓存条目和统计信息列表。
Modules(模块) 查看有效的 Chrome 扩展程序和应用的列表。
Tests(测试) 测试与特定网址的连接。
HSTS 在 HTTP 严格传输安全 (HSTS) 集中添加或删除域名,或查询当前的 HSTS 集。

HSTS 是网站强制执行 HTTPS 连接的一种方法。有关详情,请参阅 HTTP 严格传输安全

带宽(带宽) 查看自打开标签起所发送和接收的数据总量。
Prerender(预渲染) 查看处于活动状态的预渲染网站及其历史记录。
ChromeOS(Chrome 操作系统) 捕获有助于排查 Chrome 设备问题的设备日志。您可以:

  • 导入 ONC 文件:导入开放网络配置 (ONC) 文件。
  • 存储日志:将所有设备日志存储在一个 TGZ 文件中。
  • 执行网络调试:捕获特定网络界面(包括 Wi-Fi、以太网、蜂窝网络和微波存取全球互通 (WiMAX))的日志。

要了解如何检查 Chrome 设备日志,请参阅 Chrome 设备调试日志

该内容对您有帮助吗?