当前位置:首页 > 科技  > 软件

聊聊Nginx的Keepalive_time参数,你学会了吗?

来源: 责编: 时间:2023-12-06 09:20:09 337观看
导读序本文主要研究一下nginx的keepalive_time参数keepalive_timeSyntax: keepalive_time time;Default: keepalive_time 1h;Context: http, server, locationThis directive appeared in version 1.19.10.nginx的1.19.10

序本文主要研究一下nginx的keepalive_time参数eGj28资讯网——每日最新资讯28at.com

keepalive_timeSyntax: keepalive_time time;Default: keepalive_time 1h;Context: http, server, locationThis directive appeared in version 1.19.10.nginx的1.19.10版本新增了keepalive_time参数,用于限制一个keep-alive连接处理请求的最长时间。当达到这个时间后,连接会在后续请求处理完成后关闭。eGj28资讯网——每日最新资讯28at.com

ngx_http_core_modulenginx/src/http/ngx_http_core_module.ceGj28资讯网——每日最新资讯28at.com

voidngx_http_update_location_config(ngx_http_request_t *r){ngx_http_core_loc_conf_t *clcf;eGj28资讯网——每日最新资讯28at.com

//......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)eGj28资讯网——每日最新资讯28at.com

ngx_http_core_keepalivenginx/src/http/ngx_http_core_module.ceGj28资讯网——每日最新资讯28at.com

static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ngx_http_core_loc_conf_t *clcf = conf;eGj28资讯网——每日最新资讯28at.com

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_timeouteGj28资讯网——每日最新资讯28at.com

ngx_http_header_filter_modulenginx/src/http/ngx_http_header_filter_module.ceGj28资讯网——每日最新资讯28at.com

static ngx_int_tngx_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];eGj28资讯网——每日最新资讯28at.com

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,可以看到这个值是固定的eGj28资讯网——每日最新资讯28at.com

ngx_http_set_keepalivenginx/src/http/ngx_http_request.ceGj28资讯网——每日最新资讯28at.com

static voidngx_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;eGj28资讯网——每日最新资讯28at.com

//......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之后触发eGj28资讯网——每日最新资讯28at.com

ngx_http_keepalive_handlernginx/src/http/ngx_http_request.ceGj28资讯网——每日最新资讯28at.com

static voidngx_http_keepalive_handler(ngx_event_t *rev){size_t size;ssize_t n;ngx_buf_t *b;ngx_connection_t *c;eGj28资讯网——每日最新资讯28at.com

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)eGj28资讯网——每日最新资讯28at.com

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;}}eGj28资讯网——每日最新资讯28at.com

#endifeGj28资讯网——每日最新资讯28at.com

b = 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)删除定时任务eGj28资讯网——每日最新资讯28at.com

小结

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

本文链接:http://www.28at.com/showinfo-26-38528-0.html聊聊Nginx的Keepalive_time参数,你学会了吗?

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 七个鲜为人知的VS Code快捷键

下一篇: 新年高颜值装机必选,技嘉 B760M 冰雕 X(特别版)主板新品火热开售

标签:
  • 热门焦点
  • MIX Fold3包装盒泄露 新机本月登场

    小米的全新折叠屏旗舰MIX Fold3将于本月发布,近日该机的真机包装盒在网上泄露。从图上来看,新的MIX Fold3包装盒在外观设计方面延续了之前的方案,变化不大,这也是目前小米旗舰
  • 一加首款折叠屏!一加Open渲染图出炉:罕见单手可握小尺寸

    8月5日消息,此前就有爆料称,一加首款折叠屏手机将会在第三季度上市,如今随着时间临近,新机的各种消息也开始浮出水面。据悉,这款新机将会被命名为&ldquo;On
  • 每天一道面试题-CPU伪共享

    前言:了不起:又到了每天一到面试题的时候了!学弟,最近学习的怎么样啊 了不起学弟:最近学习的还不错,每天都在学习,每天都在进步! 了不起:那你最近学习的什么呢? 了不起学弟:最近在学习C
  • 使用AIGC工具提升安全工作效率

    在日常工作中,安全人员可能会涉及各种各样的安全任务,包括但不限于:开发某些安全工具的插件,满足自己特定的安全需求;自定义github搜索工具,快速查找所需的安全资料、漏洞poc、exp
  • 一条抖音4亿人围观 ! 这家MCN比无忧传媒还野

    作者:Hiu 来源:互联网品牌官01 擦边少女空降热搜,幕后推手曝光被网友誉为&ldquo;纯欲天花板&rdquo;的女网红井川里予,近期因为一组哥特风照片登上热搜,引发了一场互联网世界关于
  • 网红炒股不为了赚钱,那就是耍流氓!

    来源:首席商业评论6月26日高调宣布入市,网络名嘴大v胡锡进居然进军了股市。在一次财经媒体峰会上,几个财经圈媒体大佬就&ldquo;胡锡进炒股是否知道认真报道&rdquo;展开讨论。有
  • 携众多高端产品亮相ChinaJoy,小米带来一场科技与人文的视听盛宴

    7月28日,全球数字娱乐领域最具知名度与影响力的年度盛会中国国际数码互动娱乐展览会(简称ChinaJoy)在上海新国际博览中心盛大开幕。作为全球领先的科
  • 联想的ThinkBook Plus下一版曝光,键盘旁边塞个平板

    ThinkBook Plus 是联想的一个特殊笔记本类别,它在封面放入了一块墨水屏,也给人留下了较为深刻的印象。据有人爆料,联想的下一款 ThinkBook Plus 可能更特殊,它
  • 微软发布Windows 11新版 引入全新任务栏状态

    近日,微软发布了Windows 11新版,而Build 22563更新主要引入了几周前曝光的平板模式任务栏等,系统更流畅了。更新中,Windows 11加入了专门针对平板优化的任务栏
Top