• Nginx的http启动流程分析


    一、Nginx的http

    1、遇到conf文件的http模块。http不是在Nginx的mian函数中启动,而是解析conf文件时遇到http才会去解析并启动。
    2、请求到来时,调用http server。

    二、Nginx 启动http Server流程

    启动http server
    nginx
    ngx_module_t
    ngx_http_commands
    ngx_http_block
    ngx_http_module_ctx
    ngx_http_optimize_servers
    ngx_http_init_listening
    ngx_http_add_listening
    ngx_create_listening

    2.1、ngx_module_t

    Nginx的http是以NGX_CORE_MODULE模块的方式加载到Nginx中,因此它定义成一个ngx_module_t(/src/http/ngx_http.c):

    static ngx_command_t  ngx_http_commands[] = {
    
        { ngx_string("http"),
          NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
          ngx_http_block,
          0,
          0,
          NULL },
    
          ngx_null_command
    };
    
    
    static ngx_core_module_t  ngx_http_module_ctx = {
        ngx_string("http"),
        NULL,
        NULL
    };
    
    ngx_module_t  ngx_http_module = {
        NGX_MODULE_V1,
        &ngx_http_module_ctx,                  /* module context */
        ngx_http_commands,                     /* module directives */
        NGX_CORE_MODULE,                       /* module type */
        NULL,                                  /* init master */
        NULL,                                  /* init module */
        NULL,                                  /* init process */
        NULL,                                  /* init thread */
        NULL,                                  /* exit thread */
        NULL,                                  /* exit process */
        NULL,                                  /* exit master */
        NGX_MODULE_V1_PADDING
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    /src/core/ngx_module.h

    #define NGX_MODULE_V1                                                         \
        NGX_MODULE_UNSET_INDEX, NGX_MODULE_UNSET_INDEX,                           \
        NULL, 0, 0, nginx_version, NGX_MODULE_SIGNATURE
    
    #define NGX_MODULE_V1_PADDING  0, 0, 0, 0, 0, 0, 0, 0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.2、ngx_http_commands

    当解析conf文件时,如果遇到“http”就会获取ngx_http_commands,执行ngx_http_block函数。
    (/src/http/ngx_http.c)

    static ngx_command_t  ngx_http_commands[] = {
    
        { ngx_string("http"),
          NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
          ngx_http_block,
          0,
          0,
          NULL },
    
          ngx_null_command
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.3、ngx_http_block

    ngx_http_block相当于http的主函数,里面做了很多的初始化工作。其中在最后会调用一个ngx_http_optimize_servers的函数优化端口、地址和服务器名称列表。
    (/src/http/ngx_http.c)

    static char *
    ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    {
        char                        *rv;
        ngx_uint_t                   mi, m, s;
        ngx_conf_t                   pcf;
        ngx_http_module_t           *module;
        ngx_http_conf_ctx_t         *ctx;
        ngx_http_core_loc_conf_t    *clcf;
        ngx_http_core_srv_conf_t   **cscfp;
        ngx_http_core_main_conf_t   *cmcf;
    
        if (*(ngx_http_conf_ctx_t **) conf) {
            return "is duplicate";
        }
    
        /* the main http context */
    
        ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
        if (ctx == NULL) {
            return NGX_CONF_ERROR;
        }
    
        *(ngx_http_conf_ctx_t **) conf = ctx;
    
    
        /* count the number of the http modules and set up their indices */
    
        ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);
    
    
        /* the http main_conf context, it is the same in the all http contexts */
    
        ctx->main_conf = ngx_pcalloc(cf->pool,
                                     sizeof(void *) * ngx_http_max_module);
        if (ctx->main_conf == NULL) {
            return NGX_CONF_ERROR;
        }
    
    
        /*
         * the http null srv_conf context, it is used to merge
         * the server{}s' srv_conf's
         */
    
        ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
        if (ctx->srv_conf == NULL) {
            return NGX_CONF_ERROR;
        }
    
    
        /*
         * the http null loc_conf context, it is used to merge
         * the server{}s' loc_conf's
         */
    
        ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
        if (ctx->loc_conf == NULL) {
            return NGX_CONF_ERROR;
        }
    
    
        /*
         * create the main_conf's, the null srv_conf's, and the null loc_conf's
         * of the all http modules
         */
    
        for (m = 0; cf->cycle->modules[m]; m++) {
            if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
                continue;
            }
    
            module = cf->cycle->modules[m]->ctx;
            mi = cf->cycle->modules[m]->ctx_index;
    
            if (module->create_main_conf) {
                ctx->main_conf[mi] = module->create_main_conf(cf);
                if (ctx->main_conf[mi] == NULL) {
                    return NGX_CONF_ERROR;
                }
            }
    
            if (module->create_srv_conf) {
                ctx->srv_conf[mi] = module->create_srv_conf(cf);
                if (ctx->srv_conf[mi] == NULL) {
                    return NGX_CONF_ERROR;
                }
            }
    
            if (module->create_loc_conf) {
                ctx->loc_conf[mi] = module->create_loc_conf(cf);
                if (ctx->loc_conf[mi] == NULL) {
                    return NGX_CONF_ERROR;
                }
            }
        }
    
        pcf = *cf;
        cf->ctx = ctx;
    
        for (m = 0; cf->cycle->modules[m]; m++) {
            if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
                continue;
            }
    
            module = cf->cycle->modules[m]->ctx;
    
            if (module->preconfiguration) {
                if (module->preconfiguration(cf) != NGX_OK) {
                    return NGX_CONF_ERROR;
                }
            }
        }
    
        /* parse inside the http{} block */
    
        cf->module_type = NGX_HTTP_MODULE;
        cf->cmd_type = NGX_HTTP_MAIN_CONF;
        rv = ngx_conf_parse(cf, NULL);
    
        if (rv != NGX_CONF_OK) {
            goto failed;
        }
    
        /*
         * init http{} main_conf's, merge the server{}s' srv_conf's
         * and its location{}s' loc_conf's
         */
    
        cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
        cscfp = cmcf->servers.elts;
    
        for (m = 0; cf->cycle->modules[m]; m++) {
            if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
                continue;
            }
    
            module = cf->cycle->modules[m]->ctx;
            mi = cf->cycle->modules[m]->ctx_index;
    
            /* init http{} main_conf's */
    
            if (module->init_main_conf) {
                rv = module->init_main_conf(cf, ctx->main_conf[mi]);
                if (rv != NGX_CONF_OK) {
                    goto failed;
                }
            }
    
            rv = ngx_http_merge_servers(cf, cmcf, module, mi);
            if (rv != NGX_CONF_OK) {
                goto failed;
            }
        }
    
    
        /* create location trees */
    
        for (s = 0; s < cmcf->servers.nelts; s++) {
    
            clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
    
            if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
    
            if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
        }
    
    
        if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    
        if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    
    
        for (m = 0; cf->cycle->modules[m]; m++) {
            if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
                continue;
            }
    
            module = cf->cycle->modules[m]->ctx;
    
            if (module->postconfiguration) {
                if (module->postconfiguration(cf) != NGX_OK) {
                    return NGX_CONF_ERROR;
                }
            }
        }
    
        if (ngx_http_variables_init_vars(cf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    
        /*
         * http{}'s cf->ctx was needed while the configuration merging
         * and in postconfiguration process
         */
    
        *cf = pcf;
    
    
        if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    
    
        /* optimize the lists of ports, addresses and server names */
    
        if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    
        return NGX_CONF_OK;
    
    failed:
    
        *cf = pcf;
    
        return rv;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226

    2.4、ngx_http_optimize_servers

    ngx_http_optimize_servers里传入对应的端口启动http服务器
    (/src/http/ngx_http.c)

    static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports)
    {
        ngx_uint_t             p, a;
        ngx_http_conf_port_t  *port;
        ngx_http_conf_addr_t  *addr;
    
        if (ports == NULL) {
            return NGX_OK;
        }
    
        port = ports->elts;
        for (p = 0; p < ports->nelts; p++) {
    
            ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
                     sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);
    
            /*
             * check whether all name-based servers have the same
             * configuration as a default server for given address:port
             */
    
            addr = port[p].addrs.elts;
            for (a = 0; a < port[p].addrs.nelts; a++) {
    
                if (addr[a].servers.nelts > 1
    #if (NGX_PCRE)
                    || addr[a].default_server->captures
    #endif
                   )
                {
                    if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
                        return NGX_ERROR;
                    }
                }
            }
    
            if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
                return NGX_ERROR;
            }
        }
    
        return NGX_OK;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    2.5、ngx_http_init_listening

    在ngx_http_optimize_servers里调用ngx_http_init_listening函数监听。ngx_http_init_listening里面有一个while循环,根据端口调用ngx_http_add_listening进行监听。
    (/src/http/ngx_http.c)

    static ngx_int_t
    ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
    {
        ngx_uint_t                 i, last, bind_wildcard;
        ngx_listening_t           *ls;
        ngx_http_port_t           *hport;
        ngx_http_conf_addr_t      *addr;
    
        addr = port->addrs.elts;
        last = port->addrs.nelts;
    
        /*
         * If there is a binding to an "*:port" then we need to bind() to
         * the "*:port" only and ignore other implicit bindings.  The bindings
         * have been already sorted: explicit bindings are on the start, then
         * implicit bindings go, and wildcard binding is in the end.
         */
    
        if (addr[last - 1].opt.wildcard) {
            addr[last - 1].opt.bind = 1;
            bind_wildcard = 1;
    
        } else {
            bind_wildcard = 0;
        }
    
        i = 0;
    
        while (i < last) {
    
            if (bind_wildcard && !addr[i].opt.bind) {
                i++;
                continue;
            }
    
            ls = ngx_http_add_listening(cf, &addr[i]);
            if (ls == NULL) {
                return NGX_ERROR;
            }
    
            hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
            if (hport == NULL) {
                return NGX_ERROR;
            }
    
            ls->servers = hport;
    
            hport->naddrs = i + 1;
    
            switch (ls->sockaddr->sa_family) {
    
    #if (NGX_HAVE_INET6)
            case AF_INET6:
                if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
                    return NGX_ERROR;
                }
                break;
    #endif
            default: /* AF_INET */
                if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
                    return NGX_ERROR;
                }
                break;
            }
    
            if (ngx_clone_listening(cf, ls) != NGX_OK) {
                return NGX_ERROR;
            }
    
            addr++;
            last--;
        }
    
        return NGX_OK;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    2.6、ngx_http_add_listening

    ngx_http_add_listening里面调用ngx_create_listening传入IP地址创建listen fd进行监听。

    static ngx_listening_t *
    ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
    {
        ngx_listening_t           *ls;
        ngx_http_core_loc_conf_t  *clcf;
        ngx_http_core_srv_conf_t  *cscf;
    
        ls = ngx_create_listening(cf, &addr->opt.sockaddr.sockaddr,
                                  addr->opt.socklen);
        if (ls == NULL) {
            return NULL;
        }
    
        ls->addr_ntop = 1;
    
        ls->handler = ngx_http_init_connection;
    
        cscf = addr->default_server;
        ls->pool_size = cscf->connection_pool_size;
        ls->post_accept_timeout = cscf->client_header_timeout;
    
        clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
    
        ls->logp = clcf->error_log;
        ls->log.data = &ls->addr_text;
        ls->log.handler = ngx_accept_log_error;
    
    #if (NGX_WIN32)
        {
        ngx_iocp_conf_t  *iocpcf = NULL;
    
        if (ngx_get_conf(cf->cycle->conf_ctx, ngx_events_module)) {
            iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
        }
        if (iocpcf && iocpcf->acceptex_read) {
            ls->post_accept_buffer_size = cscf->client_header_buffer_size;
        }
        }
    #endif
    
        ls->backlog = addr->opt.backlog;
        ls->rcvbuf = addr->opt.rcvbuf;
        ls->sndbuf = addr->opt.sndbuf;
    
        ls->keepalive = addr->opt.so_keepalive;
    #if (NGX_HAVE_KEEPALIVE_TUNABLE)
        ls->keepidle = addr->opt.tcp_keepidle;
        ls->keepintvl = addr->opt.tcp_keepintvl;
        ls->keepcnt = addr->opt.tcp_keepcnt;
    #endif
    
    #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
        ls->accept_filter = addr->opt.accept_filter;
    #endif
    
    #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
        ls->deferred_accept = addr->opt.deferred_accept;
    #endif
    
    #if (NGX_HAVE_INET6)
        ls->ipv6only = addr->opt.ipv6only;
    #endif
    
    #if (NGX_HAVE_SETFIB)
        ls->setfib = addr->opt.setfib;
    #endif
    
    #if (NGX_HAVE_TCP_FASTOPEN)
        ls->fastopen = addr->opt.fastopen;
    #endif
    
    #if (NGX_HAVE_REUSEPORT)
        ls->reuseport = addr->opt.reuseport;
    #endif
    
        return ls;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    三、Nginx 启动http 状态的流程

    启动http server
    nginx
    ngx_module_t
    ngx_http_commands
    ngx_http_block
    ngx_http_module_ctx
    ngx_http_init_phase_handlers

    http状态机描述定义(/src/http/ngx_http_core_module.h):

    typedef enum {
        NGX_HTTP_POST_READ_PHASE = 0,
    
        NGX_HTTP_SERVER_REWRITE_PHASE,
    
        NGX_HTTP_FIND_CONFIG_PHASE,
        NGX_HTTP_REWRITE_PHASE,
        NGX_HTTP_POST_REWRITE_PHASE,
    
        NGX_HTTP_PREACCESS_PHASE,
    
        NGX_HTTP_ACCESS_PHASE,
        NGX_HTTP_POST_ACCESS_PHASE,
    
        NGX_HTTP_PRECONTENT_PHASE,
    
        NGX_HTTP_CONTENT_PHASE,
    
        NGX_HTTP_LOG_PHASE
    } ngx_http_phases;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

  • 相关阅读:
    YashanDB个人版正式开放下载!参与首批体验官活动赢好礼!
    【2022年11月15日提高A组】 奇数计数【数学】
    C# Thread.Sleep 不精准的问题以及解决方案
    [认知能力]电子信息类专业新生暑假可以先预习一些什么专业内容?
    8 种最坑SQL语法,工作中踩过吗?
    进程管理命令 动态监控进程 rpm yum
    JavaScript系列从入门到精通系列第七篇:JavaScrip当中的运算符,主要涉及JavaScript当中的六大数据类型的四则运算
    企业如何提供安全方面的投资回报率?
    理论第六课——二分查找函数
    数据结构之美:如何优化搜索和排序算法
  • 原文地址:https://blog.csdn.net/Long_xu/article/details/127961102