详解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 相关文章推荐
Linux安装Nginx步骤详解
Mar 31 Servers
Nginx Rewrite使用场景及配置方法解析
Apr 01 Servers
windows下快速安装nginx并配置开机自启动的方法
May 11 Servers
配置nginx 重定向到系统维护页面
Jun 08 Servers
教你利用Nginx 服务搭建子域环境提升二维地图加载性能的步骤
Sep 25 Servers
Nginx下SSL证书安装部署步骤介绍
Dec 06 Servers
idea下配置tomcat避坑详解
Apr 12 Servers
nginx搭建NFS网络文件系统
Apr 14 Servers
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
Apr 29 Servers
使用 Docker Compose 构建复杂的多容器App
Apr 30 Servers
nginx配置限速限流基于内置模块
May 02 Servers
讨论nginx location 顺序问题
May 30 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
如何使用GDB调试PHP程序
2015/12/08 PHP
老生常谈php 正则中的i,m,s,x,e分别表示什么
2017/03/02 PHP
PHP实现文件上传后台处理脚本
2020/03/04 PHP
JSON.parse 解析字符串出错的解决方法
2010/07/08 Javascript
复选框全选与全不选操作实现思路
2013/08/18 Javascript
JS.getTextContent(element,preformatted)使用介绍
2013/09/21 Javascript
jquery实现无限分级横向导航菜单的方法
2015/03/12 Javascript
js实现可折叠展开的手风琴菜单效果
2015/09/07 Javascript
Bootstrap CSS组件之下拉菜单(dropdown)
2016/12/17 Javascript
AngularJS的依赖注入实例分析(使用module和injector)
2017/01/19 Javascript
javascript 初学教程及五子棋小程序的简单实现
2017/07/04 Javascript
vue组件生命周期详解
2017/11/07 Javascript
Vue组件库发布到npm详解
2018/02/17 Javascript
JS获取本地地址及天气的方法实例小结
2019/05/10 Javascript
nodejs实现的http、https 请求封装操作示例
2020/02/06 NodeJs
python SMTP实现发送带附件电子邮件
2018/05/22 Python
python 输入一个数n,求n个数求乘或求和的实例
2018/11/13 Python
解决Python print输出不换行没空格的问题
2018/11/14 Python
windows下numpy下载与安装图文教程
2019/04/02 Python
解决Django加载静态资源失败的问题
2019/07/28 Python
解决Python列表字符不区分大小写的问题
2019/12/19 Python
浅谈matplotlib 绘制梯度下降求解过程
2020/07/12 Python
英国浴室洗脸盆购物网站:Click Basin
2018/06/08 全球购物
传播学毕业生求职信
2013/10/11 职场文书
演讲比赛获奖感言
2014/02/02 职场文书
《再见了,亲人》教学反思
2014/02/26 职场文书
小学家长评语大全
2014/04/16 职场文书
社团活动总结
2014/04/28 职场文书
学习保证书范文
2014/04/30 职场文书
优秀教师演讲稿
2014/05/06 职场文书
食品安全承诺书
2014/05/22 职场文书
伊索寓言读书笔记
2015/06/30 职场文书
高二化学教学反思
2016/02/22 职场文书
pycharm安装深度学习pytorch的d2l包失败问题解决
2022/03/25 Python
关于Python使用turtle库画任意图的问题
2022/04/01 Python
win11高清晰音频管理器在哪里?win11找不到高清晰音频管理器解决办法
2022/04/08 数码科技