nginx的keepalive相關(guān)參數(shù)使用源碼解讀
序
本文主要研究一下nginx的keepalive相關(guān)參數(shù)
keepalive_timeout
Syntax: keepalive_timeout timeout [header_timeout]; Default: keepalive_timeout 75s; Context: http, server, location
默認(rèn)是75s,客戶(hù)端的一個(gè)keep-alive連接在服務(wù)端保持open的時(shí)間,為0表示禁用keep-alive,可選指定header_timeout,若有指定則response header會(huì)有Keep-Alive: timeout=time,該header能被Mozilla和Konqueror瀏覽器識(shí)別,MSIE瀏覽器大概在60s會(huì)關(guān)閉keep-alive連接
keepalive_requests
Syntax: keepalive_requests number; Default: keepalive_requests 1000; Context: http, server, location
keepalive_requests用于指定一個(gè)keep-alive連接最大處理的請(qǐng)求數(shù),超過(guò)此值則連接被關(guān)閉
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; clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (r->method & clcf->limit_except) { r->loc_conf = clcf->limit_except_loc_conf; clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); } if (r == r->main) { ngx_set_connection_log(r->connection, clcf->error_log); } if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) { r->connection->sendfile = 1; } else { r->connection->sendfile = 0; } if (clcf->client_body_in_file_only) { r->request_body_in_file_only = 1; r->request_body_in_persistent_file = 1; r->request_body_in_clean_file = clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN; r->request_body_file_log_level = NGX_LOG_NOTICE; } else { r->request_body_file_log_level = NGX_LOG_WARN; } r->request_body_in_single_buf = clcf->client_body_in_single_buffer; 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; } } if (!clcf->tcp_nopush) { /* disable TCP_NOPUSH/TCP_CORK use */ r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED; } if (clcf->handler) { r->content_handler = clcf->handler; } }
ngx_http_core_module的ngx_http_update_location_config(ngx_http_request_t *r)方法在keepalive為true時(shí),若connection的requests的requests大于等于配置的keepalive_requests,則設(shè)置keepalive為0;若ngx_current_msec減去connection的start_time等于keepalive_time則設(shè)置keepalive為0
ngx_http_header_filter
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; } r->header_sent = 1; if (r != r->main) { return NGX_OK; } //...... if (r->keepalive && (ngx_terminate || ngx_exiting)) { r->keepalive = 0; } //...... if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) { len += sizeof("Connection: upgrade" CRLF) - 1; } else if (r->keepalive) { len += sizeof("Connection: keep-alive" CRLF) - 1; /* * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header. * MSIE keeps the connection alive for about 60-65 seconds. * Opera keeps the connection alive very long. * Mozilla keeps the connection alive for N plus about 1-10 seconds. * Konqueror keeps the connection alive for about N seconds. */ if (clcf->keepalive_header) { len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2; } } else { len += sizeof("Connection: close" CRLF) - 1; } //...... 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為1時(shí)會(huì)添加Connection: keep-alive
,若開(kāi)啟keepalive_header,則添加Keep-Alive: timeout=%T"
;若keepalive為0時(shí),則添加Connection: close
到response的header
小結(jié)
nginx提供了keepalive_timeout(一個(gè)keep-alive連接在服務(wù)端保持open的時(shí)間
)及keepalive_requests(一個(gè)keep-alive連接最大處理的請(qǐng)求數(shù)
)參數(shù),其中ngx_http_core_module的ngx_http_update_location_config(ngx_http_request_t *r)方法在keepalive為true時(shí),若connection的requests的requests大于等于配置的keepalive_requests,則設(shè)置keepalive為0;若ngx_current_msec減去connection的start_time等于keepalive_time則設(shè)置keepalive為0;而ngx_http_header_filter_module的ngx_http_header_filter方法,在keepalive為1時(shí)會(huì)添加Connection: keep-alive
,若開(kāi)啟keepalive_header,則添加Keep-Alive: timeout=%T"
;若keepalive為0時(shí),則添加Connection: close
到response的header。
以上就是nginx的keepalive相關(guān)參數(shù)使用源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于nginx keepalive參數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Linux下Tomcat+Nginx服務(wù)器環(huán)境安裝配置的簡(jiǎn)明教程
以Nginx作為反向代理再用Tomcat驅(qū)動(dòng)Java Web程序是當(dāng)今很流行的一種方案,那么這里我們就著眼于最基本的生產(chǎn)環(huán)境搭建,一起來(lái)看一下Linux下Tomcat+Nginx服務(wù)器環(huán)境安裝配置的簡(jiǎn)明教程2016-05-05Kubernetes之安裝nginx-controller作為統(tǒng)一網(wǎng)關(guān)方式
這篇文章主要介紹了Kubernetes之安裝nginx-controller作為統(tǒng)一網(wǎng)關(guān)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07fastdfs+nginx集群搭建的實(shí)現(xiàn)
這篇文章主要介紹了fastdfs+nginx集群搭建的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10使用Nginx服務(wù)器如何實(shí)現(xiàn)動(dòng)靜分離和反向代理
這篇文章主要介紹了使用Nginx服務(wù)器如何實(shí)現(xiàn)動(dòng)靜分離和反向代理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Mac使用Nginx設(shè)置代理并禁用自帶Apache的問(wèn)題記錄
本文介紹如何在Mac上禁用自帶的Apache服務(wù)并安裝Nginx,首先需要關(guān)閉Apache并禁止其自啟動(dòng),接著,通過(guò)Homebrew安裝Nginx,并配置其文件和目錄,最后,介紹了如何生成SSL/自簽名證書(shū),詳細(xì)步驟包括修改Apache配置、安裝Nginx、編輯Nginx配置文件以及驗(yàn)證和重啟Nginx服務(wù)2024-09-09