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协议,WEB服务器本人选择的是nginx,在过去,Nginx并没有内置SPDY协议,需要打开的话还要下载开发版然后手动编译,很不方便
    2014-12-12
  • Nginx部署Vue二级目录500错误的解决方案

    Nginx部署Vue二级目录500错误的解决方案

    这段描述主要讲解了在Nginx环境下部署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

最新评论