详解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 多个location转发任意请求或访问静态资源文件的实现
Mar 31 Servers
Nginx配置80端口访问8080及项目名地址方法解析
Mar 31 Servers
Mac环境Nginx配置和访问本地静态资源的实现
Mar 31 Servers
Nginx URL重写rewrite机制原理及使用实例
Apr 01 Servers
Nginx使用X-Accel-Redirect实现静态文件下载的统计、鉴权、防盗链、限速等
Apr 04 Servers
学习nginx基础知识
Sep 04 Servers
Nginx进程调度问题详解
Sep 25 Servers
CentOS下安装Jenkins的完整步骤
Apr 07 Servers
如何Tomcat中使用ipv6地址
May 06 Servers
Apache SeaTunnel实现 非CDC数据抽取
May 20 Servers
Windows server 2012 NTP时间同步的实现
Jun 25 Servers
Flink 侧流输出源码示例解析
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
压力如何影响浓缩咖啡品质
2021/03/03 咖啡文化
PHP仿盗链代码
2012/06/03 PHP
php漏洞之跨网站请求伪造与防止伪造方法
2013/08/15 PHP
PHP文件上传操作实例详解
2016/09/27 PHP
js(JavaScript)实现TAB标签切换效果的简单实例
2014/02/26 Javascript
jQuery插件开发详细教程
2014/06/06 Javascript
JavaScript字符串对象slice方法入门实例(用于字符串截取)
2014/10/16 Javascript
ECMAScript5(ES5)中bind方法使用小结
2015/05/07 Javascript
JS实现支持多选的遍历下拉列表代码
2015/08/20 Javascript
JavaScript操作XML/HTML比较常用的对象属性集锦
2015/10/30 Javascript
基于Bootstrap+jQuery.validate实现表单验证
2016/05/30 Javascript
详解vue-router 2.0 常用基础知识点之导航钩子
2017/05/10 Javascript
微信小程序实现点击文字页面跳转功能【附源码下载】
2017/12/12 Javascript
vue添加axios,并且指定baseurl的方法
2018/09/19 Javascript
vue+element_ui上传文件,并传递额外参数操作
2020/12/05 Vue.js
[00:29]2019完美世界全国高校联赛(秋季赛)总决赛海口落幕
2019/12/10 DOTA
Python构建XML树结构的方法示例
2017/06/30 Python
Python探索之自定义实现线程池
2017/10/27 Python
Win8.1下安装Python3.6提示0x80240017错误的解决方法
2018/07/31 Python
Python chardet库识别编码原理解析
2020/02/18 Python
200行python代码实现贪吃蛇游戏
2020/04/24 Python
django为Form生成的label标签添加class方式
2020/05/20 Python
conda安装tensorflow和conda常用命令小结
2021/02/20 Python
尼克松手表官网:Nixon手表
2019/03/17 全球购物
英国和世界各地预订便宜的酒店:LateRooms.com
2019/05/05 全球购物
意大利奢侈品牌在线精品店:Jole.it
2020/11/23 全球购物
100%羊绒:NakedCashmere
2020/08/26 全球购物
结构工程个人自荐信范文
2013/11/30 职场文书
服装机修工岗位职责
2013/12/26 职场文书
施工班组长岗位职责
2014/01/05 职场文书
护理专业自我鉴定
2014/01/30 职场文书
银行贷款委托书范本
2014/10/11 职场文书
工作失职自我检讨书
2015/05/05 职场文书
公司处罚决定书
2015/06/24 职场文书
pytorch 如何使用amp进行混合精度训练
2021/05/24 Python
利用JuiceFS使MySQL 备份验证性能提升 10 倍
2022/03/17 MySQL