详解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 相关文章推荐
详解Apache SkyWalking 告警配置指南
Apr 22 Servers
nginx的zabbix 5.0安装部署的方法步骤
Jul 16 Servers
Shell脚本一键安装Nginx服务自定义Nginx版本
Mar 20 Servers
nginx共享内存的机制详解
Mar 21 Servers
CentOS下安装Jenkins的完整步骤
Apr 07 Servers
Windows server 2012 R2 安装IIS服务器
Apr 29 Servers
Nginx 常用配置
May 15 Servers
解决Vmware虚拟机安装centos8报错“Section %Packages Does Not End With %End. Pane Is Dead”
Jun 01 Servers
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
Jun 10 Servers
Tomcat 与 maven 的安装与使用教程
Jun 16 Servers
Docker部署Mysql8的实现步骤
Jul 07 Servers
windows server2016安装oracle 11g的图文教程
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
php下使用无限生命期Session的方法
2007/03/16 PHP
理解php Hash函数,增强密码安全
2011/02/25 PHP
新手菜鸟必读:session与cookie的区别
2013/08/22 PHP
PHP使用反射机制实现查找类和方法的所在位置
2016/04/22 PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
2016/05/19 PHP
Jquery知识点三 jquery表单对象操作
2011/01/17 Javascript
JS的Document属性和方法小结
2013/09/17 Javascript
jquery上传插件fineuploader上传文件使用方法(jquery图片上传插件)
2013/12/05 Javascript
在JavaScript中处理字符串之fontcolor()方法的使用
2015/06/08 Javascript
JS中处理时间之setUTCMinutes()方法的使用
2015/06/12 Javascript
JS 实现倒计时数字时钟效果【附实例代码】
2016/03/30 Javascript
jQuery插件开发汇总
2016/05/15 Javascript
快速将Vue项目升级到webpack3的方法步骤
2017/09/14 Javascript
element-ui 限制日期选择的方法(datepicker)
2018/05/16 Javascript
vue axios整合使用全攻略
2018/05/24 Javascript
详解微信小程序input标签正则初体验
2018/08/18 Javascript
微信小程序实现原生步骤条
2019/07/25 Javascript
js实现橱窗展示效果
2020/01/11 Javascript
Vue 微信端扫描二维码苹果端却只能保存图片问题(解决方法)
2020/01/19 Javascript
JavaScript canvas动画实现时钟效果
2020/02/10 Javascript
Django自带的加密算法及加密模块详解
2019/12/03 Python
记一次pyinstaller打包pygame项目为exe的过程(带图片)
2020/03/02 Python
Python中Selenium库使用教程详解
2020/07/23 Python
css3实现冲击波效果的示例代码
2018/01/11 HTML / CSS
Kenneth Cole官网:纽约时尚优雅品牌
2016/11/14 全球购物
英国性能汽车零件和发动机配件在线:Maxpeedingrods
2019/11/05 全球购物
Exception类的常用方法
2012/06/16 面试题
个人自荐信
2013/12/05 职场文书
致百米运动员广播稿
2014/01/29 职场文书
求职自荐信怎么写
2014/03/06 职场文书
党员教师工作决心书
2014/03/13 职场文书
工程部经理岗位职责
2015/02/02 职场文书
世界环境日活动总结
2015/02/11 职场文书
离婚被告代理词
2015/05/23 职场文书
毕业感言怎么写
2015/07/31 职场文书
技术入股合作协议书
2016/03/21 职场文书