Nginx學習——進程模型(master 進程)

來源:互聯網
上載者:User

標籤:

進程模型

        Nginx分為Single和Master兩種進程模型,Single模型即為單進程方式工作,具有較差的容錯能力,不適合生產之用。Master模型即為一個master進程+N個worker進程的工作方式。生產環境都是用master-worker模型來工作。


master進程

        我們知道在main函數中完成了Nginx啟動初始化過程,啟動初始化過程中的一個重要環節就是解析設定檔,回調各個配置指令的回呼函數,因此完成了各個模組的配置及相互關聯。在所有的這些重要及不重要的初始化完成後,main函數就開始為我們開啟進程的“大門”——調用ngx_master_process_cycle(cycle); 在剖析ngx_master_process_cycle(cycle)之前,我們先來看看該函數中用到的7個標誌位,如下面的表所示:


進程中接收到的訊號對Nginx架構的意義:

訊號

對應進程中的全域標誌位變數

意義

QUIT

ngx_quit

優雅地關閉整個服務

TERM或INT

ngx_terminate

強制關閉整個服務

USR1

ngx_reopen

重新開啟服務中的所有檔案

WINCH

ngx_noaccept

所有子進程不再接受處理新的串連,實際相當於對所有子進程發送QUIT訊號

USR2

ngx_change_binary

平滑升級到新版本的Nginx程式

HUP

ng_reconfigure

重讀設定檔並使服務對新配置項生效

CHLD

ngx_reap

有子進程意外結束,這時需要監控所有子進程,也就是ngx_reap_children方法所做的工作

 

        還有一個標誌位會用到:ngx_restart,它僅僅是在master工作流程中作為標誌位使用,與訊號無關。master進程就是根據這8個標誌來決定工作流程的,流程圖如下:

 

        以下是對ngx_master_process_cycle()函數源碼的剖析,可參照流程圖理解:

voidngx_master_process_cycle(ngx_cycle_t *cycle){    char              *title;    u_char            *p;    size_t             size;    ngx_int_t          i;    ngx_uint_t         n, sigio;    sigset_t           set;    struct itimerval   itv;    ngx_uint_t         live;    ngx_msec_t         delay;    ngx_listening_t   *ls;    ngx_core_conf_t   *ccf;/*屏蔽一些列訊號*/    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);    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);    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);/*建立子進程,接受請求,完成響應*/    ngx_start_worker_processes(cycle, ccf->worker_processes,                               NGX_PROCESS_RESPAWN);/*建立有關cache的子進程*/    ngx_start_cache_manager_processes(cycle, 0);    ngx_new_binary = 0;    delay = 0;    sigio = 0;    live = 1;    for ( ;; ) {        if (delay) {            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) {                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,                              "setitimer() failed");            }        }        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "sigsuspend");/*master進程掛起,等待訊號的產生*/        sigsuspend(&set);        ngx_time_update();        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,                       "wake up, sigio %i", sigio);/*ngx_reap若為1,則表示要監控所有子進程*/        if (ngx_reap) {            ngx_reap = 0;            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");/*ngx_reap_children(會遍曆ngx_process數組,檢查每個子進程的狀態,對於非正常退出的子進程會重新拉起,最後,返回一個live標誌位,如果所有的子進程都已經正常退出,則live為0,初次之外live為1。*/            live = ngx_reap_children(cycle);        }/*當live標誌為0,並且ngx_terminate或ngx_quit中的一個標誌位1時,都將調用ngx_master_process_exit方法退出master進程*/        if (!live && (ngx_terminate || ngx_quit)) {            ngx_master_process_exit(cycle);        }/*ngx_terminate標誌為1,則向所有子進程發送訊號TERM,通知子進程強制退出進程*/        if (ngx_terminate) {            if (delay == 0) {                delay = 50;            }            if (sigio) {                sigio--;                continue;            }            sigio = ccf->worker_processes + 2 /* cache processes */;//強制退出進程            if (delay > 1000) {                ngx_signal_worker_processes(cycle, SIGKILL);            } else {                ngx_signal_worker_processes(cycle,                                       ngx_signal_value(NGX_TERMINATE_SIGNAL));            }            continue;        }//優雅地退出服務,向所有子進程發送QUIT訊號,通知它們退出進程        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;        }/*重新讀取設定檔。Nginx不會讓原先的worker等子進程在從新讀取設定檔。它的策略是重新初始化ngx_cycle_t結構體,用它來讀取新的設定檔,再拉起新的worker進程,銷毀就的worker進程。*/        if (ngx_reconfigure) {            ngx_reconfigure = 0;            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;            }            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");/*調用ngx_init_cycle方法重新初始化ngx_cycle_t結構體*/            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);//啟動worker子進程            ngx_start_worker_processes(cycle, ccf->worker_processes,                                       NGX_PROCESS_JUST_RESPAWN);//啟動cache_manage子進程            ngx_start_cache_manager_processes(cycle, 1);            /* allow new processes to start */            ngx_msleep(100);            live = 1;//向原先的所有子進程發送QUIT訊號            ngx_signal_worker_processes(cycle,                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));        }/*調用ngx_start_worker_processes拉起worker進程,調用ngx_start_cache_manager_processes根據緩衝模組的請款選擇是否啟動cache manage進程或者cache loader進程,同時將live置為1,ngx_restart置為0*/        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;        }/*重新開啟所有檔案*/        if (ngx_reopen) {/*將ngx_reopen置為0*/            ngx_reopen = 0;            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");/*調用ngx_reopen_files方法重新開啟所有檔案*/            ngx_reopen_files(cycle, ccf->user);/*向所有子進程發送USR1訊號,要求子進程都重新開啟所有檔案*/            ngx_signal_worker_processes(cycle,                                        ngx_signal_value(NGX_REOPEN_SIGNAL));        }/*表示需要平滑升級Nginx*/        if (ngx_change_binary) {/*將ngx_change_binary標誌置為0*/            ngx_change_binary = 0;            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");/*調用ngx_exec_new_binary方法用新的子進程啟動新版本的Nginx程式*/            ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);        }/*向所有子進程發送QUIT訊號,要求他們優雅關閉服務*/        if (ngx_noaccept) {/*將ngx_noaccept置為0*/            ngx_noaccept = 0;/*將ngx_noaccepting置為1,表示正在停止接受新的串連*/            ngx_noaccepting = 1;/*向所有子進程發送QUIT訊號*/            ngx_signal_worker_processes(cycle,                                        ngx_signal_value(NGX_SHUTDOWN_SIGNAL));        }    }}


 

 轉載註明出處,謝謝~~

Nginx學習——進程模型(master 進程)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.