当前位置: 首页 > news >正文

聊聊nginx的keepalive_time参数

本文主要研究一下nginx的keepalive_time参数

keepalive_time

Syntax:	keepalive_time time;
Default:	
keepalive_time 1h;
Context:	http, server, location
This directive appeared in version 1.19.10.

nginx的1.19.10版本新增了keepalive_time参数,用于限制一个keep-alive连接处理请求的最长时间。当达到这个时间后,连接会在后续请求处理完成后关闭。

ngx_http_core_module

nginx/src/http/ngx_http_core_module.c

void
ngx_http_update_location_config(ngx_http_request_t *r)
{ngx_http_core_loc_conf_t  *clcf;//......if (r->keepalive) {if (clcf->keepalive_timeout == 0) {r->keepalive = 0;} else if (r->connection->requests >= clcf->keepalive_requests) {r->keepalive = 0;} else if (ngx_current_msec - r->connection->start_time> clcf->keepalive_time){r->keepalive = 0;} else if (r->headers_in.msie6&& r->method == NGX_HTTP_POST&& (clcf->keepalive_disable& NGX_HTTP_KEEPALIVE_DISABLE_MSIE6)){/** MSIE may wait for some time if an response for* a POST request was sent over a keepalive connection*/r->keepalive = 0;} else if (r->headers_in.safari&& (clcf->keepalive_disable& NGX_HTTP_KEEPALIVE_DISABLE_SAFARI)){/** Safari may send a POST request to a closed keepalive* connection and may stall for some time, see*     https://bugs.webkit.org/show_bug.cgi?id=5760*/r->keepalive = 0;}}//......
}    

ngx_http_core_module的ngx_http_update_location_config方法在开启keepalive时会判断connection的存活时间,若大于keepalive_time则关闭keepalive(ngx_current_msec - r->connection->start_time > clcf->keepalive_time)

ngx_http_core_keepalive

nginx/src/http/ngx_http_core_module.c

static char *
ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ngx_http_core_loc_conf_t *clcf = conf;ngx_str_t  *value;if (clcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {return "is duplicate";}value = cf->args->elts;clcf->keepalive_timeout = ngx_parse_time(&value[1], 0);if (clcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {return "invalid value";}if (cf->args->nelts == 2) {return NGX_CONF_OK;}clcf->keepalive_header = ngx_parse_time(&value[2], 1);if (clcf->keepalive_header == (time_t) NGX_ERROR) {return "invalid value";}return NGX_CONF_OK;
}

ngx_http_core_module的ngx_http_core_keepalive方法会解析nginx配置文件的keepalive_timeout配置,第一个参数为keepalive_timeout参数,第二参数为header_timeout

ngx_http_header_filter_module

nginx/src/http/ngx_http_header_filter_module.c

static ngx_int_t
ngx_http_header_filter(ngx_http_request_t *r)
{u_char                    *p;size_t                     len;ngx_str_t                  host, *status_line;ngx_buf_t                 *b;ngx_uint_t                 status, i, port;ngx_chain_t                out;ngx_list_part_t           *part;ngx_table_elt_t           *header;ngx_connection_t          *c;ngx_http_core_loc_conf_t  *clcf;ngx_http_core_srv_conf_t  *cscf;u_char                     addr[NGX_SOCKADDR_STRLEN];if (r->header_sent) {return NGX_OK;}//......if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {b->last = ngx_cpymem(b->last, "Connection: upgrade" CRLF,sizeof("Connection: upgrade" CRLF) - 1);} else if (r->keepalive) {b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,sizeof("Connection: keep-alive" CRLF) - 1);if (clcf->keepalive_header) {b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,clcf->keepalive_header);}} else {b->last = ngx_cpymem(b->last, "Connection: close" CRLF,sizeof("Connection: close" CRLF) - 1);}//......
}    

ngx_http_header_filter_module的ngx_http_header_filter方法在开启keepalive的时候会写入Connection: keep-alive,若keepalive_header的值大于0则写入Keep-Alive: timeout=%T,可以看到这个值是固定的

ngx_http_set_keepalive

nginx/src/http/ngx_http_request.c

static void
ngx_http_set_keepalive(ngx_http_request_t *r)
{int                        tcp_nodelay;ngx_buf_t                 *b, *f;ngx_chain_t               *cl, *ln;ngx_event_t               *rev, *wev;ngx_connection_t          *c;ngx_http_connection_t     *hc;ngx_http_core_loc_conf_t  *clcf;//......wev = c->write;wev->handler = ngx_http_empty_handler;if (b->pos < b->last) {ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");c->log->action = "reading client pipelined request line";r = ngx_http_create_request(c);if (r == NULL) {ngx_http_close_connection(c);return;}r->pipeline = 1;c->data = r;c->sent = 0;c->destroyed = 0;c->pipeline = 1;if (rev->timer_set) {ngx_del_timer(rev);}rev->handler = ngx_http_process_request_line;ngx_post_event(rev, &ngx_posted_events);return;}//......rev->handler = ngx_http_keepalive_handler;//......c->idle = 1;ngx_reusable_connection(c, 1);ngx_add_timer(rev, clcf->keepalive_timeout);if (rev->ready) {ngx_post_event(rev, &ngx_posted_events);}    
}    

ngx_http_request的ngx_http_set_keepalive方法,在b->pos < b->last会尝试读取request line然后执行ngx_http_create_request,若能读到数据则判断是否有timer,有则执行ngx_del_timer(rev)删除timer,然后返回;若进入keepalive逻辑,则会通过ngx_add_timer添加一个定时事件,在keepalive_timeout之后触发

ngx_http_keepalive_handler

nginx/src/http/ngx_http_request.c

static void
ngx_http_keepalive_handler(ngx_event_t *rev)
{size_t             size;ssize_t            n;ngx_buf_t         *b;ngx_connection_t  *c;c = rev->data;ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");if (rev->timedout || c->close) {ngx_http_close_connection(c);return;}#if (NGX_HAVE_KQUEUE)if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {if (rev->pending_eof) {c->log->handler = NULL;ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,"kevent() reported that client %V closed ""keepalive connection", &c->addr_text);
#if (NGX_HTTP_SSL)if (c->ssl) {c->ssl->no_send_shutdown = 1;}
#endifngx_http_close_connection(c);return;}}#endifb = c->buffer;size = b->end - b->start;if (b->pos == NULL) {/** The c->buffer's memory was freed by ngx_http_set_keepalive().* However, the c->buffer->start and c->buffer->end were not changed* to keep the buffer size.*/b->pos = ngx_palloc(c->pool, size);if (b->pos == NULL) {ngx_http_close_connection(c);return;}b->start = b->pos;b->last = b->pos;b->end = b->pos + size;}/** MSIE closes a keepalive connection with RST flag* so we ignore ECONNRESET here.*/c->log_error = NGX_ERROR_IGNORE_ECONNRESET;ngx_set_socket_errno(0);n = c->recv(c, b->last, size);c->log_error = NGX_ERROR_INFO;if (n == NGX_AGAIN) {if (ngx_handle_read_event(rev, 0) != NGX_OK) {ngx_http_close_connection(c);return;}/** Like ngx_http_set_keepalive() we are trying to not hold* c->buffer's memory for a keepalive connection.*/if (ngx_pfree(c->pool, b->start) == NGX_OK) {/** the special note that c->buffer's memory was freed*/b->pos = NULL;}return;}if (n == NGX_ERROR) {ngx_http_close_connection(c);return;}c->log->handler = NULL;if (n == 0) {ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,"client %V closed keepalive connection", &c->addr_text);ngx_http_close_connection(c);return;}b->last += n;c->log->handler = ngx_http_log_error;c->log->action = "reading client request line";c->idle = 0;ngx_reusable_connection(c, 0);c->data = ngx_http_create_request(c);if (c->data == NULL) {ngx_http_close_connection(c);return;}c->sent = 0;c->destroyed = 0;ngx_del_timer(rev);rev->handler = ngx_http_process_request_line;ngx_http_process_request_line(rev);
}

ngx_http_request的ngx_http_keepalive_handler会在rev->timedout || c->close的时候执行ngx_http_close_connection然后返回,若还能读到请求数据则执行ngx_del_timer(rev)删除定时任务

小结

nginx的1.19.10版本新增了keepalive_time参数(默认1h),用于限制一个keep-alive连接处理请求的最长时间(即指定connection的最大存活时间),当达到这个时间后,连接会在后续请求处理完成后关闭。而keepalive_timeout参数(默认75s)则是用于指定connection最大的空闲时间,nginx内部有会给该连接设定一个timer,在keepalive_timeout之后触发,若连接还是空闲则关闭连接。

doc

  • keepalive_time
  • What’s the difference between Nginx ‘keepalive_time’ and ‘keepalive_timeout’?

相关文章:

聊聊nginx的keepalive_time参数

序 本文主要研究一下nginx的keepalive_time参数 keepalive_time Syntax: keepalive_time time; Default: keepalive_time 1h; Context: http, server, location This directive appeared in version 1.19.10.nginx的1.19.10版本新增了keepalive_time参数&#xff0c;用于限…...

沐风老师3DMAX键盘球建模方法详解

3DMAX键盘球建模教程 本教程给大家分享一个3dMax键盘球的建模方法过程。在学习本教程之前&#xff0c;大家需要对3dMax基本操作及建模知识有所掌握&#xff0c;还是那句话&#xff1a;做实例的前提是选学习基础知识和掌握3dMax的基本操作。 下面就给大家一步一步讲解演示3dMax…...

算法通关村第一关—白银挑战—链表高频面试算法题—查找两个链表的第一个公共子节点

文章目录 查找两个链表的第一个公共子节点&#xff08;1&#xff09;暴力求解法&#xff08;2&#xff09;使用哈希Hash⭐&#xff08;3&#xff09;使用集合⭐ - 与Hash类似&#xff08;4&#xff09;使用栈⭐&#xff08;5&#xff09;仍有更多方法&#xff0c;作者尚未理解&…...

C/C++ 发送与接收HTTP/S请求

HTTP&#xff08;Hypertext Transfer Protocol&#xff09;是一种用于传输超文本的协议。它是一种无状态的、应用层的协议&#xff0c;用于在计算机之间传输超文本文档&#xff0c;通常在 Web 浏览器和 Web 服务器之间进行数据通信。HTTP 是由互联网工程任务组&#xff08;IETF…...

【算法集训】基础数据结构:一、顺序表(下)

由于今天的题目是昨天剩下的&#xff0c;所以只有两道题&#xff0c;也非常简单&#xff0c;刷完下班~~~嘿嘿 第六题 2656. K 个元素的最大和 https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/description/ 很简单的思路&#xff0c;要得到得分最大的&…...

[Java][项目][战斗逻辑]基于JFrame的文字游戏

项目注解&#xff1a; Core:启动文件 AttributeBean&#xff1a;玩家属性 BackpackedBean&#xff1a;背包设计&#xff08;未完成&#xff09; BackpackedFrame&#xff1a;背包页面&#xff08;未完成&#xff09; BattleField&#xff1a;战斗逻辑&#xff08;核心&…...

顺序表和链表面试题

文章目录 顺序表(1)原地移除数组中所有的元素val&#xff0c;要求时间复杂度为O(N)&#xff0c;空间复杂度为O(1)。(2)删除有序数组中的重复项(3)合并两个有序数组 链表(1)删除链表中等于给定值 val 的所有节点(2)反转一个单链表(3) 合并两个有序链表(4)链表的中间结点(5)链表中…...

树_二叉搜索树累加求和

//给出二叉 搜索 树的根节点&#xff0c;该树的节点值各不相同&#xff0c;请你将其转换为累加树&#xff08;Greater Sum Tree&#xff09;&#xff0c;使每个节点 node 的新值等于原树中大于或等于 // node.val 的值之和。 // // 提醒一下&#xff0c;二叉搜索树满足下列约束…...

gcc编译流程概述

前言 本篇文章介绍gcc编译器编译C文件的流程概述 比如我们创建了一个.c文件hello_gcc.c #include <stdio.h> int main() {printf("Hello gcc!!!\n");return 0; }最简单的方式就是在终端使用命令 gcc hello_gcc.c -o hello_gcc // 编译、汇编、链接 ./hello_…...

【web安全】ssrf漏洞的原理与使用

前言 菜某对ssrf漏洞的总结。 ssrf的作用 主要作用&#xff1a;访问外界无法访问的内网进行信息收集。 1.进行端口扫描&#xff0c;资源访问 2.指纹信息识别&#xff0c;访问相应的默认文件 3.利用漏洞或者和payload进一步运行其他程序 4.get类型漏洞利用&#xff0c;传参数…...

佳易王会员管理软件店铺积分以及积分兑换系统

一、佳易王会员管理软件大众版 部分功能简介&#xff1a; 1、会员信息登记 &#xff1a;可以直接使用手机号登记&#xff0c;也可以使用实体卡片&#xff0c;推荐用手机号即可。 2、会员卡类型 &#xff1a;可以自由设置卡的类型&#xff0c;比如&#xff1a;充值卡、计次卡、…...

Django回顾【二】

目录 一、Web框架 二、WSGI协议 三、 Django框架 1、MVC与MTV模型 2、Django的下载与使用 补充 3、启动django项目 补充 5、 Django请求生命周期 四、路由控制 1、路由是什么&#xff1f; 2、如何使用 3、path详细使用 4、re_path详细使用 5、反向解析 6、路由…...

[Ubuntu 18.04] RK3399搭建SSH服务实现远程访问

SSH(Secure Shell)是一种网络协议和软件,用于安全地远程登录到计算机并进行网络服务的加密通信。它提供了加密的认证和安全的数据传输,使得在不安全的网络中进行远程管理和访问变得更加安全。 以下是 SSH 服务的一些关键特点和用途: 安全认证:SSH 使用公钥/私钥加密技术…...

Linux进程间通信之共享内存

&#x1f4df;作者主页&#xff1a;慢热的陕西人 &#x1f334;专栏链接&#xff1a;Linux &#x1f4e3;欢迎各位大佬&#x1f44d;点赞&#x1f525;关注&#x1f693;收藏&#xff0c;&#x1f349;留言 本博客主要内容讲解共享内存原理和相关接口的介绍&#xff0c;以及一个…...

lv11 嵌入式开发 RTC 17

目录 1 RTC简介 ​编辑2 Exynos4412下的RTC控制器 2.1 概述 2.2 特征 2.3 功能框图 3 寄存器介绍 3.1 概述 3.2 BCD格式的年月日寄存器 3.3 INTP中断挂起寄存器 3.4 RTCCON控制寄存器 3.5 CURTICCNT 作为嘀嗒定时器使用的寄存器 4 RTC编程 5 练习 1 RTC简介 RTC(…...

c语言指针详解(上)

目录 一、指针的基本概念和用法 二、指针运算 2.1 指针的自增和自减运算 2.2 指针的自增和自减运算 三、数组和指针 四、指针和函数 4.1 在函数中使用指针作为参数和返回值 4.1.1 使用指针作为函数参数 4.1.2 使用指针作为函数返回值 4.2 指针参数的传值和传引用特性 4.2.1 指针…...

如何删除mac苹果电脑上面的流氓软件?

在使用苹果电脑的过程中&#xff0c;有时候我们也会遇到一些不需要的软件。无论是因为不再需要&#xff0c;或者是为了释放磁盘空间&#xff0c;删除这些软件是很重要的。本文将为大家介绍怎样删除苹果电脑上的软件&#xff01; CleanMyMac X全新版下载如下: https://wm.make…...

WordPress(11)给文章添加预计阅读时长

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、文件配置二、代码块1.引入库2.配置 single.php三、效果图前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了…...

周周爱学习之快速排序

快速排序&#xff0c;顾名思义&#xff0c;快速排序是一种速度非常快的一种排序算法 平均时间复杂度为O(),最坏时间复杂度为O()数据量较大时&#xff0c;优势非常明显属于不稳定排序 1.算法描述 每一轮排序选择一个基准点&#xff08;pivot&#xff09;进行分区 让小于基准点…...

国产接口测试工具APIpost

说实话&#xff0c;了解APIpost是因为&#xff0c;我的所有接口相关的文章下&#xff0c;都有该APIpost水军的评论&#xff0c;无非就是APIpost是中文版的postman&#xff0c;有多么多么好用&#xff0c;虽然咱也还不是什么啥网红&#xff0c;但是不知会一声就乱在评论区打广告…...

CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型

CVPR 2025 | MIMO&#xff1a;支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题&#xff1a;MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者&#xff1a;Yanyuan Chen, Dexuan Xu, Yu Hu…...

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする 1、前言(1)情况说明(2)工程师的信仰2、知识点(1) にする1,接续:名词+にする2,接续:疑问词+にする3,(A)は(B)にする。(2)復習:(1)复习句子(2)ために & ように(3)そう(4)にする3、…...

vscode(仍待补充)

写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh&#xff1f; debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...

基于服务器使用 apt 安装、配置 Nginx

&#x1f9fe; 一、查看可安装的 Nginx 版本 首先&#xff0c;你可以运行以下命令查看可用版本&#xff1a; apt-cache madison nginx-core输出示例&#xff1a; nginx-core | 1.18.0-6ubuntu14.6 | http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages ng…...

聊聊 Pulsar:Producer 源码解析

一、前言 Apache Pulsar 是一个企业级的开源分布式消息传递平台&#xff0c;以其高性能、可扩展性和存储计算分离架构在消息队列和流处理领域独树一帜。在 Pulsar 的核心架构中&#xff0c;Producer&#xff08;生产者&#xff09; 是连接客户端应用与消息队列的第一步。生产者…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

【git】把本地更改提交远程新分支feature_g

创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...

ardupilot 开发环境eclipse 中import 缺少C++

目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...