一、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 | 有子進程以外結束,需要監控所有子進程 |
master進程主要進行一些全局性的初始化工作和管理worker的工作;事件處理是在worker中進行的。
進程啟動的過程中,有一些重要的全局數據會被設置,最重要的是進程表ngx_processes,master每創建一個worker都會把一個設置好的ngx_process_t結構變量放入ngx_processes中,進程表長度為1024,剛創建的進程存放在ngx_process_slot位置,ngx_last_process是進程表中最后一個存量進程的下一個位置,ngx_process_t是進程在nginx中的抽象:
typedef void (*ngx_spawn_proc_pt) (ngx_cycle_t *cycle, void *data);typedef struct {ngx_pid_t pid; //進程idint status; //進程狀態ngx_socket_t channel[2]; //socketpair創建的socket句柄 ngx_spawn_proc_pt proc; //進程執行函數void *data; //執行函數的參數char *name; //名稱 unsigned respawn:1; //重新創建unsigned just_spawn:1; //第一次創建的unsigned detached:1; //分離的unsigned exiting:1; //正在退出的unsigned exited:1; //退出過的 } ngx_process_t;
?ngx_master_process_cycle中完成ngx的進程啟動過程:
1.master進程設置一些需要處理的信號量
如何殺nginx進程、2.調用ngx_setproctitle設置進程標題
3.調用ngx_start_worker_processes啟動worker進程
4.調用ngx_start_cache_manager_processes啟動文件cache管理進程
5.master進程循環處理信號量,從而實現管理worker子進程
void ngx_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;//信號處理設置工作,master設置一些需要處理的信號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);//調用ngx_setproctilte設置進程標題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);//其中包含了fork產生子進程的內容,調用ngx_start_worker_processes(cycle, ccf->worker_processes, NGX_PROCESS_RESPAWN)啟動worker進程ngx_start_worker_processes(cycle, ccf->worker_processes,NGX_PROCESS_RESPAWN);//Cache管理進程與cache加載進程的主流程,調用ngx_start_cache_manager_processes(cycle, 0)啟動文件cache管理進程,有些模塊需要文件cachengx_start_cache_manager_processes(cycle, 0);ngx_new_binary = 0;delay = 0;sigio = 0;live = 1;//master循環處理信號量for ( ;; ) {//循環
//delay用來設置等待worker推出的時間,master接受了退出信號后,
//首先發送退出信號給worker,而worker退出需要一些時間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");sigsuspend(&set);//master進程休眠,等待接受信號被激活 ngx_time_update();ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,"wake up, sigio %i", sigio);//標志位為1表示需要監控所有子進程,收到了SIGCHLD信號,有worker退出(ngx_reap?==?1)if (ngx_reap) {ngx_reap = 0;ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");//處理所有worker,如果有worker異常退出,則重啟這個worker,如果所有的worker都退出了,則返回0live = ngx_reap_children(cycle);//管理子進程 }//當live標志位為0(表示所有子進程已經退出)、ngx_terminate標志位為1或者ngx_quit標志位為1表示要退出master進程if (!live && (ngx_terminate || ngx_quit)) {ngx_master_process_exit(cycle);//退出master進程 }//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) {
//延時已到,給所有worker發送SIGKILL信號,強制殺死workerngx_signal_worker_processes(cycle, SIGKILL);} else {
//給所有worker發送SIGTERM信號,通知worker退出ngx_signal_worker_processes(cycle,ngx_signal_value(NGX_TERMINATE_SIGNAL));}continue;}//ngx_quit標志位為1,優雅的關閉服務if (ngx_quit) {ngx_signal_worker_processes(cycle,ngx_signal_value(NGX_SHUTDOWN_SIGNAL));//向所有子進程發送quit信號 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;}//ngx_reconfigure標志位為1,重新讀取配置文件//nginx不會讓原來的worker子進程再重新讀取配置文件,其策略是重新初始化ngx_cycle_t結構體,用它來讀取新的額配置文件//再創建新的額worker子進程,銷毀舊的worker子進程if (ngx_reconfigure) {ngx_reconfigure = 0;//ngx_new_binary標志位為1,平滑升級Nginxif (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_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);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_restart標志位在ngx_noaccepting(表示正在停止接受新的連接)為1的時候被設置為1.//重啟子進程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;}//ngx_reopen標志位為1,重新打開所有文件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));}//平滑升級Nginxif (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);}//ngx_noaccept為1,表示所有子進程不再處理新的連接,worker退出,master不退出if (ngx_noaccept) {ngx_noaccept = 0;ngx_noaccepting = 1;ngx_signal_worker_processes(cycle,ngx_signal_value(NGX_SHUTDOWN_SIGNAL));}} }
ngx_start_worker_processes函數:
函數作用:循環創建n個worker子進程
UNIX/LINUX。循環中的具體步驟:
1.在ngx_spawn_process中創建好一個worker進程
2.master進程將剛創建的worker進程的pid、worker進程在ngx_processes數組中的位置及channel[0]傳遞給前面已經創建好的worker進程,其中channel用于進程間通信
3.繼續循環開始創建下一個worker進程
static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, ngx_int_t type) {ngx_int_t i;ngx_channel_t ch;ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start worker processes");//傳遞給其他worker子進程的命令,打開通信管道ch.command = NGX_CMD_OPEN_CHANNEL;//循環創建n個worker子進程for (i = 0; i < n; i++) {//完成fok新進程的具體工作 ngx_spawn_process(cycle, ngx_worker_process_cycle,(void *) (intptr_t) i, "worker process", type);//全局數組ngx_processes就是用來存儲每個子進程的相關信息,如:pid,channel,進程做具體事情的接口指針等等,這些信息就是用結構體ngx_process_t來描述的。ch.pid = ngx_processes[ngx_process_slot].pid;ch.slot = ngx_process_slot;ch.fd = ngx_processes[ngx_process_slot].channel[0];/*在ngx_spawn_process創建好一個worker進程返回后,master進程就將worker進程的pid、worker進程在ngx_processes數組中的位置及channel[0]傳遞給前面已經創建好的worker進程,然后繼續循環開始創建下一個worker進程。剛提到一個channel[0],這里簡單說明一下:channel就是一個能夠存儲2個整型元素的數組而已,這個channel數組就是用于socketpair函數創建一個進程間通道之用的。master和worker進程以及worker進程之間都可以通過這樣的一個通道進行通信,這個通道就是在ngx_spawn_process函數中fork之前調用socketpair創建的。*/ngx_pass_open_channel(cycle, &ch);} }
ngx_spawn_process函數:
ngx_spawn_process用于具體創建worker進程。
NGINX負載均衡、步驟:
1.在進程表中為將要創建的子進程分配一個表項,若分配失敗,則出錯
2.創建父子進程間通信的socket對,設置channel
3.創建子進程:設置進程id,進程運行執行函數,設置好進程表項字段
//參數解釋: //cycle:nginx框架所圍繞的核心結構體 //proc:子進程中將要執行的工作循環 //data:參數 //name:子進程名字 ngx_pid_t ngx_spawn_process(ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data,char *name, ngx_int_t respawn) {u_long on;ngx_pid_t pid;ngx_int_t s;//將要創建的子進程在進程表中的位置//替換進程ngx_processes[respawn],可安全重用該進程表項if (respawn >= 0) {s = respawn;} else {//先找到一個被回收的進程表項for (s = 0; s < ngx_last_process; s++) {if (ngx_processes[s].pid == -1) {break;}}//進程表已經滿if (s == NGX_MAX_PROCESSES) {ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,"no more than %d processes can be spawned",NGX_MAX_PROCESSES);return NGX_INVALID_PID;}}if (respawn != NGX_PROCESS_DETACHED) {/* Solaris 9 still has no AF_LOCAL *///創建父子進程間通信的套接字對(基于TCP)if (socketpair(AF_UNIX, SOCK_STREAM, 0, ngx_processes[s].channel) == -1){ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"socketpair() failed while spawning \"%s\"", name);return NGX_INVALID_PID;}ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,"channel %d:%d",ngx_processes[s].channel[0],ngx_processes[s].channel[1]);//設置socket為非阻塞模式if (ngx_nonblocking(ngx_processes[s].channel[0]) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,ngx_nonblocking_n " failed while spawning \"%s\"",name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}if (ngx_nonblocking(ngx_processes[s].channel[1]) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,ngx_nonblocking_n " failed while spawning \"%s\"",name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}//開啟channel[0]的消息驅動IOon = 1;if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"ioctl(FIOASYNC) failed while spawning \"%s\"", name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}//設置channel[0]的宿主,控制channel[0]的SIGIO信號只發給這個進程if (fcntl(ngx_processes[s].channel[0], F_SETOWN, ngx_pid) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"fcntl(F_SETOWN) failed while spawning \"%s\"", name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}//若進程執行了exec后,關閉socketif (fcntl(ngx_processes[s].channel[0], F_SETFD, FD_CLOEXEC) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"fcntl(FD_CLOEXEC) failed while spawning \"%s\"",name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}//同上if (fcntl(ngx_processes[s].channel[1], F_SETFD, FD_CLOEXEC) == -1) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"fcntl(FD_CLOEXEC) failed while spawning \"%s\"",name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;}ngx_channel = ngx_processes[s].channel[1];} else {ngx_processes[s].channel[0] = -1;ngx_processes[s].channel[1] = -1;}//設置當前子進程的進程表索引值ngx_process_slot = s;//創建子進程pid = fork();switch (pid) {case -1:ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,"fork() failed while spawning \"%s\"", name);ngx_close_channel(ngx_processes[s].channel, cycle->log);return NGX_INVALID_PID;case 0:ngx_pid = ngx_getpid();//設置當前子進程的進程idproc(cycle, data);//子進程運行執行函數?break;default:break;}ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start %s %P", name, pid);//設置一些進程表項字段ngx_processes[s].pid = pid;ngx_processes[s].exited = 0;//如果是重復創建,即為替換進程,不用設置其他進程表字段,直接返回。if (respawn >= 0) {return pid;}ngx_processes[s].proc = proc;ngx_processes[s].data = data;ngx_processes[s].name = name;ngx_processes[s].exiting = 0;//設置進程表項的一些狀態字switch (respawn) {case NGX_PROCESS_NORESPAWN:ngx_processes[s].respawn = 0;ngx_processes[s].just_spawn = 0;ngx_processes[s].detached = 0;break;case NGX_PROCESS_JUST_SPAWN:ngx_processes[s].respawn = 0;ngx_processes[s].just_spawn = 1;ngx_processes[s].detached = 0;break;case NGX_PROCESS_RESPAWN:ngx_processes[s].respawn = 1;ngx_processes[s].just_spawn = 0;ngx_processes[s].detached = 0;break;case NGX_PROCESS_JUST_RESPAWN:ngx_processes[s].respawn = 1;ngx_processes[s].just_spawn = 1;ngx_processes[s].detached = 0;break;case NGX_PROCESS_DETACHED:ngx_processes[s].respawn = 0;ngx_processes[s].just_spawn = 0;ngx_processes[s].detached = 1;break;}if (s == ngx_last_process) {ngx_last_process++;}return pid; }
worker進程
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) {ngx_int_t worker = (intptr_t) data;ngx_uint_t i;ngx_connection_t *c;ngx_process = NGX_PROCESS_WORKER;//子進程初始化 ngx_worker_process_init(cycle, worker);ngx_setproctitle("worker process");//這里有一段多線程條件下的代碼。由于nginx并不支持多線程,因此刪除掉了//循環for ( ;; ) {//ngx_exiting標志位為1,進程退出if (ngx_exiting) {c = cycle->connections;for (i = 0; i < cycle->connection_n; i++) {if (c[i].fd != -1 && c[i].idle) {c[i].close = 1;c[i].read->handler(c[i].read);}}if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel){ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");ngx_worker_process_exit(cycle);}}ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");ngx_process_events_and_timers(cycle);//處理事件的方法//強制結束進程if (ngx_terminate) {ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");ngx_worker_process_exit(cycle);}//優雅地退出進程if (ngx_quit) {ngx_quit = 0;ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,"gracefully shutting down");ngx_setproctitle("worker process is shutting down");if (!ngx_exiting) {ngx_close_listening_sockets(cycle);//設置ngx_exiting 標志位ngx_exiting = 1;}}//重新打開所有文件if (ngx_reopen) {ngx_reopen = 0;ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");ngx_reopen_files(cycle, -1);}} }
?