最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nginx的keepalive相關參數使用源碼解讀

 更新時間:2023年12月05日 09:23:09   作者:codecraft  
這篇文章主要為大家介紹了nginx的keepalive相關參數使用源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下nginx的keepalive相關參數

keepalive_timeout

Syntax:    keepalive_timeout timeout [header_timeout];
Default:    
keepalive_timeout 75s;
Context:    http, server, location
默認是75s,客戶端的一個keep-alive連接在服務端保持open的時間,為0表示禁用keep-alive,可選指定header_timeout,若有指定則response header會有Keep-Alive: timeout=time,該header能被Mozilla和Konqueror瀏覽器識別,MSIE瀏覽器大概在60s會關閉keep-alive連接

keepalive_requests

Syntax:    keepalive_requests number;
Default:    
keepalive_requests 1000;
Context:    http, server, location
keepalive_requests用于指定一個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;
    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時,若connection的requests的requests大于等于配置的keepalive_requests,則設置keepalive為0;若ngx_current_msec減去connection的start_time等于keepalive_time則設置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時會添加Connection: keep-alive,若開啟keepalive_header,則添加Keep-Alive: timeout=%T";若keepalive為0時,則添加Connection: close到response的header

小結

nginx提供了keepalive_timeout(一個keep-alive連接在服務端保持open的時間)及keepalive_requests(一個keep-alive連接最大處理的請求數)參數,其中ngx_http_core_module的ngx_http_update_location_config(ngx_http_request_t *r)方法在keepalive為true時,若connection的requests的requests大于等于配置的keepalive_requests,則設置keepalive為0;若ngx_current_msec減去connection的start_time等于keepalive_time則設置keepalive為0;而ngx_http_header_filter_module的ngx_http_header_filter方法,在keepalive為1時會添加Connection: keep-alive,若開啟keepalive_header,則添加Keep-Alive: timeout=%T";若keepalive為0時,則添加Connection: close到response的header。

doc keepalive_timeout

以上就是nginx的keepalive相關參數使用源碼解讀的詳細內容,更多關于nginx keepalive參數的資料請關注腳本之家其它相關文章!

相關文章

  • nginx中的proxy_redirect的使用案例詳解

    nginx中的proxy_redirect的使用案例詳解

    proxy_redirect 該指令用來修改被代理服務器返回的響應頭中的Location頭域和“refresh”頭域,這篇文章主要介紹了nginx中的proxy_redirect的使用案例詳解,需要的朋友可以參考下
    2024-06-06
  • nginx前端部署后,訪問不到同一臺機器的后端問題

    nginx前端部署后,訪問不到同一臺機器的后端問題

    這篇文章主要介紹了nginx前端部署后,訪問不到同一臺機器的后端問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nginx服務器做負載均衡反向代理的超級攻略

    Nginx服務器做負載均衡反向代理的超級攻略

    這篇文章主要介紹了Nginx服務器做負載均衡反向代理的超級攻略,包括緩存的相關設定以及負載均衡的一些常見問題的解決,極力推薦!需要的朋友可以參考下
    2015-08-08
  • 為Nginx添加SPDY功能

    為Nginx添加SPDY功能

    我也開始嘗試著給自己的論壇加上SPDY協(xié)議,WEB服務器本人選擇的是nginx,在過去,Nginx并沒有內置SPDY協(xié)議,需要打開的話還要下載開發(fā)版然后手動編譯,很不方便
    2014-12-12
  • Nginx部署Vue二級目錄500錯誤的解決方案

    Nginx部署Vue二級目錄500錯誤的解決方案

    這段描述主要講解了在Nginx環(huán)境下部署Vue項目的方法,特別針對二級域名和路徑配置進行了詳細說明,提供了解決500錯誤的有效配置
    2026-05-05
  • nginx 配置跨域失效修復的方法示例

    nginx 配置跨域失效修復的方法示例

    這篇文章主要介紹了nginx 配置跨域失效修復的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • nginx 部署前端vue項目的過程詳解

    nginx 部署前端vue項目的過程詳解

    Nginx是一款輕量級的HTTP服務器,采用事件驅動的異步非阻塞處理方式框架,這讓其具有極好的IO性能,時常用于服務端的反向代理和負載均衡,本文給大家介紹nginx 部署前端vue項目的過程,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • Nginx配置SSL證書出現PEM_read_bio_PrivateKey() failed錯誤解決

    Nginx配置SSL證書出現PEM_read_bio_PrivateKey() failed錯誤解決

    在配置GoDaddy的SSL證書并啟動Nginx時,可能遇到由于證書密鑰文件編碼問題導致的啟動失敗,具體表現為nginx報錯:PEM_read_bio_PrivateKey() failed,本文就來介紹一下,感興趣的可以了解學習
    2024-10-10
  • 使用nginx如何解決Access-Control-Allow-Origin問題

    使用nginx如何解決Access-Control-Allow-Origin問題

    這篇文章主要介紹了使用nginx如何解決Access-Control-Allow-Origin問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nginx負載均衡/SSL配置的實現

    Nginx負載均衡/SSL配置的實現

    這篇文章主要介紹了Nginx負載均衡/SSL配置的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論

镇平县| 延安市| 安庆市| 满城县| 松江区| 江口县| 高碑店市| 雷州市| 南平市| 从江县| 诸城市| 松江区| 洞头县| 浠水县| 陕西省| 海淀区| 嵊州市| 定远县| 昭平县| 施甸县| 伊宁市| 固原市| 鹤岗市| 木兰县| 东港市| 沁源县| 乳源| 夏河县| 馆陶县| 泽普县| 行唐县| 西贡区| 黄平县| 定兴县| 南木林县| 奉新县| 威远县| 莲花县| 花莲市| 兴宁市| 延安市|