详解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的实现
Mar 31 Servers
Filebeat 采集 Nginx 日志的方法
Mar 31 Servers
Nginx配置之实现多台服务器负载均衡
Aug 02 Servers
Apache POI的基本使用详解
Nov 07 Servers
Apache Hudi数据布局黑科技降低一半查询时间
Mar 31 Servers
Nginx虚拟主机的配置步骤过程全解
Mar 31 Servers
使用Docker容器部署rocketmq单机的全过程
Apr 03 Servers
Apache Hudi 加速传统的批处理模式
Apr 24 Servers
Windows Server 2019 安装DHCP服务及相关配置
Apr 28 Servers
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
May 11 Servers
Docker与K8s关系介绍不会Docker也可以使用K8s
Jun 25 Servers
服务器nginx权限被拒绝解决案例
Sep 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
PHPMYADMIN导入数据最大为2M的解决方法
2012/04/23 PHP
php简单获取文件扩展名的方法
2015/03/24 PHP
php操作路径的经典方法(必看篇)
2016/10/04 PHP
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
js 固定悬浮效果实现思路代码
2013/08/02 Javascript
jquery css 设置table的奇偶行背景色示例
2014/06/03 Javascript
JavaScript实现关键字高亮功能
2014/11/12 Javascript
js实现支持手机滑动切换的轮播图片效果实例
2015/04/29 Javascript
jquery插件EasyUI中form表单提交实例分享
2016/01/11 Javascript
学JavaScript七大注意事项【必看】
2016/05/04 Javascript
将html页面保存成图片,图片写入pdf的实现方法(推荐)
2016/09/17 Javascript
JavaScript中localStorage对象存储方式实例分析
2017/01/12 Javascript
JavaScript判断浏览器和hack滚动条的写法
2017/07/23 Javascript
canvas绘制爱心的几种方法总结(推荐)
2017/10/31 Javascript
常用的9个JavaScript图表库详解
2017/12/19 Javascript
vue实现提示保存后退出的方法
2018/03/15 Javascript
浅谈vue项目打包优化策略
2018/09/29 Javascript
微信小程序BindTap快速连续点击目标页面跳转多次问题处理
2019/04/08 Javascript
JavaScript模块管理的简单实现方式详解
2019/06/15 Javascript
如何在JavaScript中谨慎使用代码注释
2019/06/21 Javascript
Python中shutil模块的常用文件操作函数用法示例
2016/07/05 Python
Python中遇到的小问题及解决方法汇总
2017/01/11 Python
浅谈python中列表、字符串、字典的常用操作
2017/09/19 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
Python3.6连接Oracle数据库的方法详解
2018/05/18 Python
Python 统计位数为偶数的数字代码详解
2020/03/15 Python
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
2021/02/16 Python
芬兰攀岩、山地运动和户外活动用品购物网站:Bergfreunde
2016/10/06 全球购物
伯利陶器:Burleigh Pottery
2018/01/03 全球购物
Sisley法国希思黎美国官方网站:享誉全球的奢华植物美容品牌
2020/06/27 全球购物
个人自我评价和职业目标
2014/01/24 职场文书
30岁生日感言
2014/01/25 职场文书
设计师个人求职信范文
2014/02/02 职场文书
关于九一八事变的演讲稿2014
2014/09/17 职场文书
软件测试专业推荐信
2014/09/18 职场文书
综合素质评价自我评价
2015/03/06 职场文书