用户工具

站点工具


about_ngx_4

Nginx master进程工作原理

以下过程都在ngx_master_process_cycle函数中进行:

1.启动过程

1.1暂时阻塞所有ngx需要处理的信号

sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigaddset(&set, SIGALRM);
    sigaddset(&set, SIGIO);
    sigaddset(&set, SIGINT);
    sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
    sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
    sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL));
    sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
    sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
    sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
 
    if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                      "sigprocmask() failed");
    }
 
    sigemptyset(&set);

1.2设置进程名称

size = sizeof(master_process);
 
    for (i = 0; i < ngx_argc; i++) {
        size += ngx_strlen(ngx_argv[i]) + 1;
    }
 
    title = ngx_pnalloc(cycle->pool, size);
 
    p = ngx_cpymem(title, master_process, sizeof(master_process) - 1);
    for (i = 0; i < ngx_argc; i++) {
        *p++ = ' ';
        p = ngx_cpystrn(p, (u_char *) ngx_argv[i], size);
    }
 
    ngx_setproctitle(title);

1.3启动工作进程

ngx_start_worker_processes(cycle, ccf->worker_processes,
                               NGX_PROCESS_RESPAWN);

1.4启动cache管理进程

ngx_start_cache_manager_processes(cycle, 0);

1.5进入循环开始处理相关信号

 for ( ;; ) {
   ...
   ...
   ...
   }

2.master进程工作过程

2.1设置work进程退出等待时间

            if (ngx_sigalrm) {
                sigio = 0;
                delay *= 2;
                ngx_sigalrm = 0;
            }
 
            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                           "termination cycle: %d", delay);
 
            itv.it_interval.tv_sec = 0;
            itv.it_interval.tv_usec = 0;
            itv.it_value.tv_sec = delay / 1000;
            itv.it_value.tv_usec = (delay % 1000 ) * 1000;
 
            if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {////设置定时器,以系统真实时间来计算,送出SIGALRM信号
                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                              "setitimer() failed");
            }
 

2.2挂起,等待新的信号来临

sigsuspend(&set);

2.3更新时间

ngx_time_update();

2.4如果有worker进程因为SIGCHLD信号退出了,则重启worker进程

 if (ngx_reap) {
            ngx_reap = 0;
            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
 
            live = ngx_reap_children(cycle);
        }

2.5 master进程退出

如果所有worker进程都退出了,并且收到SIGTERM信号或SIGINT信号或SIGQUIT信号等,master进程开始处理退出

if (!live && (ngx_terminate || ngx_quit)) {
            ngx_master_process_exit(cycle);
        }

2.6处理SIGTERM信号

if (ngx_terminate) {
            if (delay == 0) {
                delay = 50;
            }
 
            if (sigio) {
                sigio--;
                continue;
            }
 
            sigio = ccf->worker_processes + 2 /* cache processes */;
 
            if (delay > 1000) { //延时大于1000,给所有worker进程发送SIGKILL,强制杀死所有worker进程
                ngx_signal_worker_processes(cycle, SIGKILL);
            } else {
                ngx_signal_worker_processes(cycle,
                                       ngx_signal_value(NGX_TERMINATE_SIGNAL));
            }
 
            continue;
        }
 

2.7处理SIGQUIT信号,并且关闭socket

if (ngx_quit) {
            ngx_signal_worker_processes(cycle,
                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
 
            ls = cycle->listening.elts;
            for (n = 0; n < cycle->listening.nelts; n++) {
                if (ngx_close_socket(ls[n].fd) == -1) {
                    ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
                                  ngx_close_socket_n " %V failed",
                                  &ls[n].addr_text);
                }
            }
            cycle->listening.nelts = 0;
 
            continue;
        }

2.8处理SIGHUP信号

2.8.1平滑升级,重启worker进程

 if (ngx_new_binary) {
                ngx_start_worker_processes(cycle, ccf->worker_processes,
                                           NGX_PROCESS_RESPAWN);
                ngx_start_cache_manager_processes(cycle, 0);
                ngx_noaccepting = 0;
 
                continue;
            }

2.8.2不是平滑升级,需要重新读取配置

cycle = ngx_init_cycle(cycle);
            if (cycle == NULL) {
                cycle = (ngx_cycle_t *) ngx_cycle;
                continue;
            }
 
            ngx_cycle = cycle;
            ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
                                                   ngx_core_module);
            ngx_start_worker_processes(cycle, ccf->worker_processes,
                                       NGX_PROCESS_JUST_RESPAWN);
            ngx_start_cache_manager_processes(cycle, 1);
 
            /* allow new processes to start */
            ngx_msleep(100);
 
            live = 1;
            ngx_signal_worker_processes(cycle,
                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
        }

2.9处理重启

 if (ngx_restart) {
            ngx_restart = 0;
            ngx_start_worker_processes(cycle, ccf->worker_processes,
                                       NGX_PROCESS_RESPAWN);
            ngx_start_cache_manager_processes(cycle, 0);
            live = 1;
        }

2.10处理SIGUSR1信号

重新打开所有文件

if (ngx_reopen) {
            ngx_reopen = 0;
            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
            ngx_reopen_files(cycle, ccf->user);
            ngx_signal_worker_processes(cycle,
                                        ngx_signal_value(NGX_REOPEN_SIGNAL));
        }

2.11处理SIGUSR2信号

热代码替换,执行新的程序

if (ngx_change_binary) {
            ngx_change_binary = 0;
            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");
            ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);
        }

2.12处理SIGWINCH信号,不再处理任何请求

 if (ngx_noaccept) {
            ngx_noaccept = 0;
            ngx_noaccepting = 1;
            ngx_signal_worker_processes(cycle,
                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
        }
about_ngx_4.txt · 最后更改: 2018/10/14 15:31 (外部编辑)