详解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实现上游服务器动态自动上下线无需reload的实现方法
Mar 31 Servers
本地通过nginx配置反向代理的全过程记录
Mar 31 Servers
基于Nginx实现限制某IP短时间访问次数
Mar 31 Servers
nginx对http请求处理的各个阶段详析
Mar 31 Servers
nginx 反向代理之 proxy_pass的实现
Mar 31 Servers
解决xampp安装后Apache无法启动
Mar 21 Servers
微信告警的zabbix监控系统 监控整个NGINX集群
Apr 18 Servers
Tomcat执行startup.bat出现闪退的原因及解决办法
Apr 20 Servers
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
Jun 10 Servers
windows server2008 开启端口的实现方法
Jun 25 Servers
本地搭建minio文件服务器(使用bat脚本启动)的方法
Jul 15 Servers
Nginx报错104:Connection reset by peer问题的解决及分析
Jul 23 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中对数组的一些常用的增、删、插操作函数总结
2015/11/27 PHP
CakePHP框架Session设置方法分析
2017/02/23 PHP
PHP实现的防止跨站和xss攻击代码【来自阿里云】
2018/01/29 PHP
点击文章内容处弹出页面代码
2009/10/01 Javascript
JavaScript小技巧 2.5 则
2010/09/12 Javascript
多个datatable共存造成多个表格的checkbox都被选中
2013/07/11 Javascript
使用GruntJS构建Web程序之合并压缩篇
2014/06/06 Javascript
运用jQuery定时器的原理实现banner图片切换
2014/10/22 Javascript
酷炫jQuery全屏3D焦点图动画效果
2016/03/22 Javascript
jQuery前端开发35个小技巧
2016/05/24 Javascript
jQuery select自动选中功能实现方法分析
2016/11/28 Javascript
微信小程序 label 组件详解及简单实例
2017/01/10 Javascript
JS正则表达式验证账号、手机号、电话和邮箱是否合法
2017/03/08 Javascript
JavaWeb表单及时验证功能在输入后立即验证(含用户类型,性别,爱好...的验证)
2017/06/09 Javascript
浅谈react+es6+webpack的基础配置
2017/08/09 Javascript
基于JavaScript实现抽奖系统
2018/01/16 Javascript
Vue 实现拖动滑块验证功能(只有css+js没有后台验证步骤)
2018/08/24 Javascript
vue+koa2实现session、token登陆状态验证的示例
2019/08/30 Javascript
在Python的Django框架中创建和使用模版
2015/07/15 Python
Python读取图片属性信息的实现方法
2016/09/11 Python
python3.4用循环往mysql5.7中写数据并输出的实现方法
2017/06/20 Python
Java及python正则表达式详解
2017/12/27 Python
Pycharm连接远程服务器并实现远程调试的实现
2019/08/02 Python
python lambda函数及三个常用的高阶函数
2020/02/05 Python
一文了解python 3 字符串格式化 F-string 用法
2020/03/04 Python
Ubuntu20下的Django安装的方法步骤
2021/01/24 Python
函授自我鉴定范文
2014/02/06 职场文书
党的群众路线教育实践活动心得体会
2014/03/03 职场文书
保险公司晨会主持词
2014/03/22 职场文书
环境整治工作方案
2014/05/18 职场文书
反对四风问题自我剖析材料
2014/09/29 职场文书
《夸父追日》教学反思
2016/02/20 职场文书
2016年小学六一儿童节活动总结
2016/04/06 职场文书
MySQL中存储时间的最佳实践指南
2021/07/01 MySQL
vue中 this.$set的使用详解
2021/11/17 Vue.js
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技