当前位置: 首页 > 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;但是不知会一声就乱在评论区打广告…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库&#xff0c;特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

Linux-07 ubuntu 的 chrome 启动不了

文章目录 问题原因解决步骤一、卸载旧版chrome二、重新安装chorme三、启动不了&#xff0c;报错如下四、启动不了&#xff0c;解决如下 总结 问题原因 在应用中可以看到chrome&#xff0c;但是打不开(说明&#xff1a;原来的ubuntu系统出问题了&#xff0c;这个是备用的硬盘&a…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

重启Eureka集群中的节点,对已经注册的服务有什么影响

先看答案&#xff0c;如果正确地操作&#xff0c;重启Eureka集群中的节点&#xff0c;对已经注册的服务影响非常小&#xff0c;甚至可以做到无感知。 但如果操作不当&#xff0c;可能会引发短暂的服务发现问题。 下面我们从Eureka的核心工作原理来详细分析这个问题。 Eureka的…...

蓝桥杯 冶炼金属

原题目链接 &#x1f527; 冶炼金属转换率推测题解 &#x1f4dc; 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V&#xff0c;是一个正整数&#xff0c;表示每 V V V 个普通金属 O O O 可以冶炼出 …...

算法:模拟

1.替换所有的问号 1576. 替换所有的问号 - 力扣&#xff08;LeetCode&#xff09; ​遍历字符串​&#xff1a;通过外层循环逐一检查每个字符。​遇到 ? 时处理​&#xff1a; 内层循环遍历小写字母&#xff08;a 到 z&#xff09;。对每个字母检查是否满足&#xff1a; ​与…...

GO协程(Goroutine)问题总结

在使用Go语言来编写代码时&#xff0c;遇到的一些问题总结一下 [参考文档]&#xff1a;https://www.topgoer.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine.html 1. main()函数默认的Goroutine 场景再现&#xff1a; 今天在看到这个教程的时候&#xff0c;在自己的电…...

【从零开始学习JVM | 第四篇】类加载器和双亲委派机制(高频面试题)

前言&#xff1a; 双亲委派机制对于面试这块来说非常重要&#xff0c;在实际开发中也是经常遇见需要打破双亲委派的需求&#xff0c;今天我们一起来探索一下什么是双亲委派机制&#xff0c;在此之前我们先介绍一下类的加载器。 目录 ​编辑 前言&#xff1a; 类加载器 1. …...