详解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 比 Apache 更牛逼
Mar 31 Servers
nginx优化的六点方法
Mar 31 Servers
nginx location中多个if里面proxy_pass的方法
Mar 31 Servers
Nginx配置80端口访问8080及项目名地址方法解析
Mar 31 Servers
Nginx 反向代理解决跨域问题多种情况分析
Jan 18 Servers
Nginx的基本概念和原理
Mar 21 Servers
CentOS安装Nginx并部署vue
Apr 12 Servers
Windows Server 2019 安装DHCP服务及相关配置
Apr 28 Servers
Windows Server 2012配置DNS服务器的方法
Apr 29 Servers
Windows Server 2008配置防火墙策略详解
Jun 28 Servers
Docker部署Mysql8的实现步骤
Jul 07 Servers
Apache自带的ab压力测试工具的实现
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服务端图片生成缩略图的方法详解
2013/06/20 PHP
PHP中VC6、VC9、TS、NTS版本的区别与用法详解
2013/10/26 PHP
手把手编写PHP框架 深入了解MVC运行流程
2016/09/19 PHP
PHP单例模式详解及实例代码
2016/12/21 PHP
ThinkPHP开发--使用七牛云储存
2017/09/14 PHP
javascript 新浪背投广告实现代码
2009/07/07 Javascript
JavaScript基础知识之数据类型
2012/08/06 Javascript
jQuery之自动完成组件的深入解析
2013/06/19 Javascript
鼠标选择动态改变网页背景颜色的JS代码
2013/12/10 Javascript
JavaScript设计模式之外观模式实例
2014/10/10 Javascript
JavaScript遍历求解数独问题的主要思路小结
2016/06/12 Javascript
全面了解函数声明与函数表达式、变量提升
2016/08/09 Javascript
JavaScript利用闭包实现模块化
2017/01/13 Javascript
JS二叉树的简单实现方法示例
2017/04/05 Javascript
Bootstrap Table使用整理(二)
2017/06/09 Javascript
js插件实现图片滑动验证码
2020/09/29 Javascript
JS获取月的第几周和年的第几周实例代码
2018/12/05 Javascript
小程序云开发实现数据库异步操作同步化
2019/05/18 Javascript
angular *Ngif else用法详解
2020/12/15 Javascript
[09:40]DAC2018 4.5 SOLO赛 MidOne vs Miracle
2018/04/06 DOTA
Python中使用语句导入模块或包的机制研究
2015/03/30 Python
使用Python的Scrapy框架编写web爬虫的简单示例
2015/04/17 Python
在Apache服务器上同时运行多个Django程序的方法
2015/07/22 Python
Python 列表理解及使用方法
2017/10/27 Python
Django框架多表查询实例分析
2018/07/04 Python
Python语言检测模块langid和langdetect的使用实例
2019/02/19 Python
python无序链表删除重复项的方法
2020/01/17 Python
浅谈HTML5 FileReader分布读取文件以及其方法简介
2017/11/09 HTML / CSS
办公室主任先进事迹
2014/01/18 职场文书
大二学生学习个人自我评价
2014/01/19 职场文书
2014公司党员自我评价范文
2014/09/11 职场文书
学生抄作业检讨书(2篇)
2014/10/17 职场文书
行政诉讼答辩状
2015/05/21 职场文书
Spring Boot项目传参校验的最佳实践指南
2022/04/05 Java/Android
MySQL创建管理HASH分区
2022/04/13 MySQL
教你如何用cmd快速登录服务器
2022/06/10 Servers