日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

聊聊Nginx的Keepalive_time參數(shù),你學(xué)會(huì)了嗎?

來(lái)源: 責(zé)編: 時(shí)間:2023-12-06 09:20:09 307觀看
導(dǎo)讀序本文主要研究一下nginx的keepalive_time參數(shù)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參數(shù)PiF28資訊網(wǎng)——每日最新資訊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參數(shù),用于限制一個(gè)keep-alive連接處理請(qǐng)求的最長(zhǎng)時(shí)間。當(dāng)達(dá)到這個(gè)時(shí)間后,連接會(huì)在后續(xù)請(qǐng)求處理完成后關(guān)閉。PiF28資訊網(wǎng)——每日最新資訊28at.com

ngx_http_core_modulenginx/src/http/ngx_http_core_module.cPiF28資訊網(wǎng)——每日最新資訊28at.com

voidngx_http_update_location_config(ngx_http_request_t *r){ngx_http_core_loc_conf_t *clcf;PiF28資訊網(wǎng)——每日最新資訊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時(shí)會(huì)判斷connection的存活時(shí)間,若大于keepalive_time則關(guān)閉keepalive(ngx_current_msec - r->connection->start_time > clcf->keepalive_time)PiF28資訊網(wǎng)——每日最新資訊28at.com

ngx_http_core_keepalivenginx/src/http/ngx_http_core_module.cPiF28資訊網(wǎng)——每日最新資訊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;PiF28資訊網(wǎng)——每日最新資訊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方法會(huì)解析nginx配置文件的keepalive_timeout配置,第一個(gè)參數(shù)為keepalive_timeout參數(shù),第二參數(shù)為header_timeoutPiF28資訊網(wǎng)——每日最新資訊28at.com

ngx_http_header_filter_modulenginx/src/http/ngx_http_header_filter_module.cPiF28資訊網(wǎng)——每日最新資訊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];PiF28資訊網(wǎng)——每日最新資訊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的時(shí)候會(huì)寫入Connection: keep-alive,若keepalive_header的值大于0則寫入Keep-Alive: timeout=%T,可以看到這個(gè)值是固定的PiF28資訊網(wǎng)——每日最新資訊28at.com

ngx_http_set_keepalivenginx/src/http/ngx_http_request.cPiF28資訊網(wǎng)——每日最新資訊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;PiF28資訊網(wǎng)——每日最新資訊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會(huì)嘗試讀取request line然后執(zhí)行ngx_http_create_request,若能讀到數(shù)據(jù)則判斷是否有timer,有則執(zhí)行ngx_del_timer(rev)刪除timer,然后返回;若進(jìn)入keepalive邏輯,則會(huì)通過ngx_add_timer添加一個(gè)定時(shí)事件,在keepalive_timeout之后觸發(fā)PiF28資訊網(wǎng)——每日最新資訊28at.com

ngx_http_keepalive_handlernginx/src/http/ngx_http_request.cPiF28資訊網(wǎng)——每日最新資訊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;PiF28資訊網(wǎng)——每日最新資訊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)PiF28資訊網(wǎng)——每日最新資訊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;}}PiF28資訊網(wǎng)——每日最新資訊28at.com

#endifPiF28資訊網(wǎng)——每日最新資訊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會(huì)在rev->timedout || c->close的時(shí)候執(zhí)行ngx_http_close_connection然后返回,若還能讀到請(qǐng)求數(shù)據(jù)則執(zhí)行ngx_del_timer(rev)刪除定時(shí)任務(wù)PiF28資訊網(wǎng)——每日最新資訊28at.com

小結(jié)

nginx的1.19.10版本新增了keepalive_time參數(shù)(默認(rèn)1h),用于限制一個(gè)keep-alive連接處理請(qǐng)求的最長(zhǎng)時(shí)間(即指定connection的最大存活時(shí)間),當(dāng)達(dá)到這個(gè)時(shí)間后,連接會(huì)在后續(xù)請(qǐng)求處理完成后關(guān)閉。而keepalive_timeout參數(shù)(默認(rèn)75s)則是用于指定connection最大的空閑時(shí)間,nginx內(nèi)部有會(huì)給該連接設(shè)定一個(gè)timer,在keepalive_timeout之后觸發(fā),若連接還是空閑則關(guān)閉連接。PiF28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-38528-0.html聊聊Nginx的Keepalive_time參數(shù),你學(xué)會(huì)了嗎?

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 七個(gè)鮮為人知的VS Code快捷鍵

下一篇: 新年高顏值裝機(jī)必選,技嘉 B760M 冰雕 X(特別版)主板新品火熱開售

標(biāo)簽:
  • 熱門焦點(diǎn)
  • 6月安卓手機(jī)好評(píng)榜:魅族20 Pro蟬聯(lián)冠軍

    性能榜和性價(jià)比榜之后,我們來(lái)看最后的安卓手機(jī)好評(píng)榜,數(shù)據(jù)來(lái)源安兔兔評(píng)測(cè),收集時(shí)間2023年6月1日至6月30日,僅限國(guó)內(nèi)市場(chǎng)。第一名:魅族20 Pro好評(píng)率:95%5月份的時(shí)候魅族20 Pro就是
  • 6月安卓手機(jī)性價(jià)比榜:Note 12 Turbo斷層式碾壓

    6月份有一個(gè)618,雖然這是京東周年慶的日子,但別的電商也都不約而同的跟進(jìn)了,反正促銷沒壞處,廠商和用戶都能滿意。618期間一些產(chǎn)品也出現(xiàn)了歷史低價(jià),那么各個(gè)價(jià)位段的產(chǎn)品性價(jià)比
  • 5月安卓手機(jī)好評(píng)榜:魅族20 Pro奪冠

    性能榜和性價(jià)比榜之后,我們來(lái)看最后的安卓手機(jī)好評(píng)榜,數(shù)據(jù)來(lái)源安兔兔評(píng)測(cè),收集時(shí)間2023年5月1日至5月31日,僅限國(guó)內(nèi)市場(chǎng)。第一名:魅族20 Pro好評(píng)率:97.50%不得不感慨魅族老品牌還
  • 摸魚心法第一章——和配置文件說(shuō)拜拜

    為了能摸魚我們團(tuán)隊(duì)做了容器化,但是帶來(lái)的問題是服務(wù)配置文件很麻煩,然后大家在群里進(jìn)行了“親切友好”的溝通圖片圖片圖片圖片對(duì)比就對(duì)比,簡(jiǎn)單對(duì)比下獨(dú)立配置中心和k8s作為配
  • 2023 年的 Node.js 生態(tài)系統(tǒng)

    隨著技術(shù)的不斷演進(jìn)和創(chuàng)新,Node.js 在 2023 年達(dá)到了一個(gè)新的高度。Node.js 擁有一個(gè)龐大的生態(tài)系統(tǒng),可以幫助開發(fā)人員更快地實(shí)現(xiàn)復(fù)雜的應(yīng)用。本文就來(lái)看看 Node.js 最新的生
  • 華為和江淮汽車合作開發(fā)百萬(wàn)元問界MPV?雙方回應(yīng)來(lái)了

    8月1日消息,郭明錤今天在社交平臺(tái)發(fā)文稱,華為正在和江淮汽車合作,開發(fā)售價(jià)在100萬(wàn)元的問界MPV,預(yù)計(jì)在2024年第2季度量產(chǎn),銷量目標(biāo)為上市首年交付5萬(wàn)輛。
  • iQOO Neo8系列或定檔5月23日:首發(fā)天璣9200+ 安卓跑分王者

    去年10月,iQOO推出了iQOO Neo7系列機(jī)型,不僅搭載了天璣9000+,而且是同價(jià)位唯一一款天璣9000+直屏旗艦,一經(jīng)上市便受到了用戶的廣泛關(guān)注。在時(shí)隔半年后,
  • AI藝術(shù)欣賞體驗(yàn)會(huì)在上海梅賽德斯奔馳中心音樂俱樂部上演

    光影交錯(cuò)的鏡像世界,虛實(shí)幻化的視覺奇觀,虛擬偶像與真人共同主持,這些場(chǎng)景都出現(xiàn)在2019世界人工智能大會(huì)的舞臺(tái)上。8月29日至31日,“AI藝術(shù)欣賞體驗(yàn)會(huì)”在上海
  • 榮耀Magic4 至臻版 首創(chuàng)智慧隱私通話 強(qiáng)勁影音系統(tǒng)

    2022年第一季度臨近尾聲,在該季度內(nèi),許多品牌陸續(xù)發(fā)布自己的最新產(chǎn)品,讓大家從全新的角度來(lái)了解當(dāng)今的手機(jī)技術(shù)。手機(jī)是電子設(shè)備中,更新迭代十分迅速的一款產(chǎn)品,基
Top 主站蜘蛛池模板: 合川市| 志丹县| 章丘市| 乌鲁木齐县| 井冈山市| 永泰县| 新乐市| 科技| 衡南县| 嘉兴市| 宣威市| 通河县| 都安| 浦东新区| 专栏| 乌拉特前旗| 通渭县| 宽城| 长治市| 包头市| 淮南市| 新津县| 垫江县| 巴塘县| 古浪县| 呼伦贝尔市| 石楼县| 隆昌县| 平武县| 新蔡县| 庆安县| 淮北市| 铜川市| 和平县| 建平县| 曲靖市| 霍城县| 集安市| 安阳县| 麻栗坡县| 临潭县|