详解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服务器如何设置url链接
Mar 31 Servers
Nginx如何配置Http、Https、WS、WSS的方法步骤
May 11 Servers
图文详解Nginx版本平滑升级方案
Sep 15 Servers
nginx内存池源码解析
Nov 20 Servers
Nginx下SSL证书安装部署步骤介绍
Dec 06 Servers
Apache Hudi集成Spark SQL操作hide表
Mar 31 Servers
Nginx配置根据url参数重定向
Apr 11 Servers
Windows Server 2019 域控制器安装图文教程
Apr 28 Servers
win server2012 r2服务器共享文件夹如何设置
Jun 21 Servers
Windows7下FTP搭建图文教程
Aug 05 Servers
Tomcat安装使用及部署Web项目的3种方法汇总
Aug 14 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中加密解密函数与DES加密解密实例
2014/10/17 PHP
PHP从FLV文件获取视频预览图的方法
2015/03/12 PHP
微信支付扫码支付php版
2016/07/22 PHP
jquery中ajax学习笔记一
2011/10/16 Javascript
jquery图片延迟加载 前端开发技能必备系列
2012/06/18 Javascript
解析javascript系统错误:-1072896658的解决办法
2013/07/08 Javascript
JavaScript的事件绑定(方便不支持js的时候)
2013/10/01 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
jquery中的$(document).ready()使用小结
2014/02/14 Javascript
input:checkbox多选框实现单选效果跟radio一样
2014/06/16 Javascript
使用jQuery将多条数据插入模态框的实现代码
2014/10/08 Javascript
动态加载jQuery的方法
2015/06/16 Javascript
实例解析jQuery工具函数
2016/12/01 Javascript
AngularJS开发教程之控制器之间的通信方法分析
2016/12/25 Javascript
详解jQuery获取特殊属性的值以及设置内容
2018/11/14 jQuery
如何使用 vue + d3 画一棵树
2018/12/03 Javascript
js+html实现周岁年龄计算器
2019/06/25 Javascript
手写Vue源码之数据劫持示例详解
2021/01/04 Vue.js
构建Python包的五个简单准则简介
2015/06/15 Python
Python3中使用PyMongo的方法详解
2017/07/28 Python
python 借助numpy保存数据为csv格式的实现方法
2018/07/04 Python
python 多线程将大文件分开下载后在合并的实例
2018/11/09 Python
详解python深浅拷贝区别
2019/06/24 Python
python list转置和前后反转的例子
2019/08/26 Python
Python同时迭代多个序列的方法
2020/07/28 Python
使用CSS3的appearance属性改变元素的外观的方法
2015/12/12 HTML / CSS
美国流行背包品牌:JanSport(杰斯伯)
2018/03/02 全球购物
外贸业务员的岗位职责
2013/11/23 职场文书
护理专业自荐信范文
2014/02/26 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
个人四风问题原因分析及整改措施
2014/09/28 职场文书
公司仓库管理制度
2015/08/04 职场文书
终止合同协议书范本
2016/03/22 职场文书
导游词之贵州织金洞
2019/10/12 职场文书
提升Nginx性能的一些建议
2021/03/31 Servers
Mysql中常用的join连接方式
2022/05/11 MySQL