Nginx 启动初始化过程

Nginx 启动过程

Nginx 的启动初始化由 main 函数完成,该函数是整个 Nginx 的入口,该函数完成 Nginx 启动初始化任务,也是所有功能模块的入口。Nginx 的初始化工作主要是一个类型为 ngx_cycle_t 类型的全局变量。main 函数定义在文件:src/​core/​nginx.c

Nginx 启动过程如下。

  • 调用 ngx_get_options() 解析命令参数;
  • 显示版本号与帮助信息;
  • 调用 ngx_time_init() 初始化并更新时间;
  • 调用 ngx_log_init() 初始化日志;
  • 创建全局变量 init_cycle 的内存池 pool;
  • 调用 ngx_save_argv() 保存命令行参数至全局变量ngx_os_argv、ngx_argc、ngx_argv 中;
  • 调用 ngx_process_options() 初始化 init_cycle 的 prefix, conf_prefix, conf_file, conf_param 等字段;
  • 调用 ngx_os_init() 初始化系统相关变量;
  • 调用 ngx_crc32_table_init() 初始化CRC表;
  • 调用 ngx_add_inherited_sockets() 继承 sockets;
  • 通过环境变量 NGINX 完成 socket 的继承,将其保存在全局变量 init_cycle 的 listening 数组中;
  • 初始化每个模块 module 的index,并计算 ngx_max_module;
  • 调用 ngx_init_cycle() 进行初始化全局变量 init_cycle,这个步骤非常重要;
  • 调用 ngx_signal_process() 处理进程信号;
  • 调用 ngx_init_signals() 注册相关信号;
  • 若无继承 sockets,则调用 ngx_daemon() 创建守护进程,并设置其标志;
  • 调用 ngx_create_pidfile() 创建进程 ID 记录文件;
  • 进入进程处理:
    • 单进程工作模式;
    • 多进程工作模式,即 master-worker 多进程工作模式;

int ngx_cdecl
main(int argc, char *const *argv)
{
    ngx_int_t         i;
    ngx_log_t        *log;
    ngx_cycle_t      *cycle, init_cycle;
    ngx_core_conf_t  *ccf;

    ngx_debug_init();

    if (ngx_strerror_init() != NGX_OK) {
        return 1;
    }

    /* 解析命令行参数 */
    if (ngx_get_options(argc, argv) != NGX_OK) {
        return 1;
    }

    /* 显示版本号与帮助信息 */
    if (ngx_show_version) {
        ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);

        if (ngx_show_help) {
            ngx_write_stderr(
                "Usage: nginx [-?hvVtq] [-s signal] [-c filename] "
                             "[-p prefix] [-g directives]" NGX_LINEFEED
                             NGX_LINEFEED
                "Options:" NGX_LINEFEED
                "  -?,-h         : this help" NGX_LINEFEED
                "  -v            : show version and exit" NGX_LINEFEED
                "  -V            : show version and configure options then exit"
                                   NGX_LINEFEED
                "  -t            : test configuration and exit" NGX_LINEFEED
                "  -q            : suppress non-error messages "
                                   "during configuration testing" NGX_LINEFEED
                "  -s signal     : send signal to a master process: "
                                   "stop, quit, reopen, reload" NGX_LINEFEED
#ifdef NGX_PREFIX
                "  -p prefix     : set prefix path (default: "
                                   NGX_PREFIX ")" NGX_LINEFEED
#else
                "  -p prefix     : set prefix path (default: NONE)" NGX_LINEFEED
#endif
                "  -c filename   : set configuration file (default: "
                                   NGX_CONF_PATH ")" NGX_LINEFEED
                "  -g directives : set global directives out of configuration "
                                   "file" NGX_LINEFEED NGX_LINEFEED
                );
        }

        if (ngx_show_configure) {
            ngx_write_stderr(
#ifdef NGX_COMPILER
                "built by " NGX_COMPILER NGX_LINEFEED
#endif
#if (NGX_SSL)
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
                "TLS SNI support enabled" NGX_LINEFEED
#else
                "TLS SNI support disabled" NGX_LINEFEED
#endif
#endif
                "configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
        }

        if (!ngx_test_config) {
            return 0;
        }
    }

    /* TODO */ ngx_max_sockets = -1;

    /* 初始化并更新时间 */
    ngx_time_init();

#if (NGX_PCRE)
    ngx_regex_init();
#endif

    ngx_pid = ngx_getpid();

    /* 初始化日志信息 */
    log = ngx_log_init(ngx_prefix);
    if (log == NULL) {
        return 1;
    }

    /* STUB */
#if (NGX_OPENSSL)
    ngx_ssl_init(log);
#endif

    /*
     * init_cycle->log is required for signal handlers and
     * ngx_process_options()
     */

    /* 全局变量init_cycle清零,并创建改变量的内存池pool */
    ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
    init_cycle.log = log;
    ngx_cycle = &init_cycle;

    init_cycle.pool = ngx_create_pool(1024, log);
    if (init_cycle.pool == NULL) {
        return 1;
    }

    /* 保存命令行参数至全局变量ngx_os_argv、ngx_argc、ngx_argv */
    if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
        return 1;
    }

    /* 初始化全局变量init_cycle中的成员:prefix、conf_prefix、conf_file、conf_param 等字段 */
    if (ngx_process_options(&init_cycle) != NGX_OK) {
        return 1;
    }

    /* 初始化系统相关变量,如:内存页面大小ngx_pagesize、最大连接数ngx_max_sockets等 */
    if (ngx_os_init(log) != NGX_OK) {
        return 1;
    }

    /*
     * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
     */

    /* 初始化 CRC 表(循环冗余校验表) */
    if (ngx_crc32_table_init() != NGX_OK) {
        return 1;
    }

    /* 通过环境变量NGINX完成socket的继承,将其保存在全局变量init_cycle的listening数组中 */
    if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
        return 1;
    }

    /* 初始化每个模块module的index,并计算ngx_max_module */
    ngx_max_module = 0;
    for (i = 0; ngx_modules[i]; i++) {
        ngx_modules[i]->index = ngx_max_module++;
    }

    /* 初始化全局变量init_cycle ,这里很重要 */
    cycle = ngx_init_cycle(&init_cycle);
    if (cycle == NULL) {
        if (ngx_test_config) {
            ngx_log_stderr(0, "configuration file %s test failed",
                           init_cycle.conf_file.data);
        }

        return 1;
    }

    if (ngx_test_config) {
        if (!ngx_quiet_mode) {
            ngx_log_stderr(0, "configuration file %s test is successful",
                           cycle->conf_file.data);
        }

        return 0;
    }

    /* 信号处理 */
    if (ngx_signal) {
        return ngx_signal_process(cycle, ngx_signal);
    }

    ngx_os_status(cycle->log);

    ngx_cycle = cycle;

    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

    if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
        ngx_process = NGX_PROCESS_MASTER;
    }

#if !(NGX_WIN32)

    /* 初始化信号,注册相关信号 */
    if (ngx_init_signals(cycle->log) != NGX_OK) {
        return 1;
    }

    /* 若无socket继承,则创建守护进程,并设置守护进程标志 */
    if (!ngx_inherited && ccf->daemon) {
       
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值