详解Nginx的超时keeplive_timeout配置步骤


Posted in Servers onMay 25, 2022

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 30s;

客户度连接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;
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;
lingering_timeout 5s;
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;
resolver_timeout 30s;
Sets a timeout for name resolution, for example:
resolver_timeout 5s;

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

Syntax: send_timeout time;
send_timeout 60s;
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;
proxy_connect_timeout 60s;
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;
proxy_read_timeout 60s;
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;
proxy_send_timeout 60s;
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.

到此这篇关于详解Nginx的超时keeplive_timeout配置步骤的文章就介绍到这了!


Tags in this post...

Servers 相关文章推荐
nginx配置proxy_pass中url末尾带/与不带/的区别详解
Mar 31 Servers
Nginx工作原理和优化总结。
Apr 02 Servers
nginx实现动静分离的方法示例
Nov 07 Servers
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
Apr 07 Servers
Windows Server 2019 安装DHCP服务及相关配置
Apr 28 Servers
Apache SeaTunnel实现 非CDC数据抽取
May 20 Servers
ubuntu下常用apt命令介绍
Jun 05 Servers
Windows Server 2008配置防火墙策略详解
Jun 28 Servers
Zabbix对Kafka topic积压数据监控的问题(bug优化)
Jul 07 Servers
Zabbix对Kafka topic积压数据监控的解决方案
Jul 07 Servers
搭建zabbix监控以及邮件报警的超级详细教学
Jul 15 Servers
Windows Server 2012 R2服务器安装与配置的完整步骤
Jul 15 Servers
nginx 配置指令之location使用详解
May 25 #Servers
zabbix配置nginx监控的实现
May 25 #Servers
nginx 添加http_stub_status_module模块
May 25 #Servers
docker 制作mysql镜像并自动安装
May 20 #Servers
阿里云服务器Ubuntu 20.04上安装Odoo 15
May 20 #Servers
Apache SeaTunnel实现 非CDC数据抽取
May 20 #Servers
apache ftpserver搭建ftp服务器
May 20 #Servers
You might like
DOTA2 玩家自创拉野攻略 特色英雄快速成长篇
2020/04/20 DOTA
PHP 数组遍历顺序理解
2009/09/09 PHP
PHP二维数组排序的3种方法和自定义函数分享
2014/04/09 PHP
php缓冲输出实例分析
2015/01/05 PHP
php递归函数三种实现方法及如何实现数字累加
2015/08/07 PHP
php实现批量删除挂马文件及批量替换页面内容完整实例
2016/07/08 PHP
PHP实现SMTP邮件的发送实例
2018/09/27 PHP
Laravel框架之解决前端显示图片问题
2019/10/24 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
2019/11/26 PHP
最新的10款jQuery内容滑块插件分享
2011/09/18 Javascript
9行javascript代码获取QQ群成员具体实现
2013/10/16 Javascript
jquery中的查找parents与closest方法之间的区别
2013/12/02 Javascript
js将控件隐藏及display属性的使用介绍
2013/12/30 Javascript
js实现字符串转日期格式的方法
2015/05/20 Javascript
javascript实现拖放效果
2015/12/16 Javascript
AngularJS 指令的交互详解及实例代码
2016/09/14 Javascript
JS插件plupload.js实现多图上传并显示进度条
2016/11/29 Javascript
iscroll.js滚动加载实例详解
2017/07/18 Javascript
Javascript实现动态时钟效果
2018/11/17 Javascript
手把手教你 CKEDITOR 4 扩展插件制作
2019/06/18 Javascript
解决vue 表格table列求和的问题
2019/11/06 Javascript
JavaScript中ES6规范中let和const的用法和区别
2020/08/06 Javascript
vant自定义二级菜单操作
2020/11/02 Javascript
Vue ​v-model相关知识总结
2021/01/28 Vue.js
[01:08:29]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第一场 1月9日
2021/03/11 DOTA
Python编码时应该注意的几个情况
2013/03/04 Python
Python之多线程爬虫抓取网页图片的示例代码
2018/01/10 Python
Python数据处理篇之Sympy系列(五)---解方程
2019/10/12 Python
基于Python3.6中的OpenCV实现图片色彩空间的转换
2020/02/03 Python
夜大自我鉴定
2013/10/31 职场文书
函授大专自我鉴定
2013/11/01 职场文书
推广普通话演讲稿
2014/05/23 职场文书
室内趣味活动方案
2014/08/24 职场文书
2014年仓管员工作总结
2014/11/18 职场文书
先进班集体申报材料
2014/12/26 职场文书
纪录片信仰观后感
2015/06/08 职场文书