[Nginx] Start Process

Source: Internet
Author: User
This process starts from the main function after the application is started.
Parse the command line parameters and save them to the ngx_cycle_t struct. The configuration file path will be saved in the ngx_process_options function.
Call the ngx_add_inherited_sockets function to obtain information about smooth upgrade in environment variables. During smooth upgrade, the old master process will send some information to the new master process through the environment variable. After the new master process starts, it will retrieve the information from the environment variable, this is the role of this function.
Call ngx_init_cycle to initialize the ngx_cycle_t struct, including:
  • Call the create_conf method of each core module to create a struct that stores configuration item parameters. Note that the struct of the non-core module storage configuration item is not created here, but is created by the core module.
  • Call the parsing method provided by the configuration module to traverse the configuration file. For each configuration item that traverses all core modules, the configuration items that the core module is interested in are stored in the ngx_command_t structure, find the hexing module that is interested in this configuration item and call the parsing method defined in ngx_command_t.
  • Call the init_conf method of all core modules and initialize them based on the parsed configuration item parameters.
  • Create and open the required directories and files.
  • Initializes the shared memory used for inter-process communication.
  • Call the ngx_open_listening_sockets function to open the listening port read from the configuration file by each nginx module. All listening ports are stored in the ngx_cycle_t-> listening array. The storage element is the ngx_listening_t struct, indicating the listening port and related parameters.
  • Call the init_module method of all modules. This method is saved in the ngx_module_t-> init_module function pointer.
If the configuration file is set to the single-process working mode (master_process is off), the ngx_single_process_cycle function is called. Call the init_process method of all modules in this function. This method is saved in the ngx_module_t-> init_process function pointer. The system enters the single-process mode and runs cyclically.
If the configuration file is set to master-worker working mode (master_process is on), The ngx_master_process_cycle function is called. This function fork (encapsulated in the ngx_start_worker_processes function) outputs the work sub-process. The work process enters the ngx_worker_process_cycle function. This function first calls the ngx_worker_process_init method, which calls the init_process method of all modules. The work process then cyclically works in ngx_worker_process_cycle.
Let's look at the overall flowchart:

Worker process workflow the worker process continues to run in the for loop of the ngx_worker_process_cycle function. It checks four flags:
  • Ngx_terminate: indicates that the process is forcibly disabled. The ngx_signal_handler function sets this parameter to 1.
  • Ngx_quit: indicates that the process is gracefully shut down. The ngx_signal_handler function sets the value to 1.
  • Ngx_reopen: indicates that the file is re-opened. The ngx_signal_handler function sets 1 by the signal processing function.
  • Ngx_exiting: indicates that the process is to be exited. Set this parameter only when the value is 0 and ngx_quit is set.
The relationship between the flag and the signal is as follows:
The Worker Process checks the preceding four flags to determine the program execution flow. The Code is as follows:
Static voidngx_worker_process_cycle (ngx_cycle_t * cycle, void * Data) {ngx_int_t worker = (intptr_t) data; ngx_uint_t I; ngx_connection_t * C; /* This function calls the init_process method of all modules */ngx_worker_process_init (cycle, worker );... for (;) {/* exit process ID */If (ngx_exiting) {c = Cycle-> connections;/* close all connections */for (I = 0; I <cycle-> connection_n; I ++) {If (C [I]. FD! =-1 & C [I]. idle) {C [I]. close = 1;/* set the close ID to 1 */C [I]. read-> handler (C [I]. read);/* call the read Event Processing Method */} If (ngx_event_timer_rbtree.root = ngx_event_timer_rbtree.sentinel) {/* The Red/black tree is empty and all events have been processed, call the exit_process Method */ngx_worker_process_exit (cycle) of all modules;}/* There are also events to be processed, continue to execute down */ngx_process_events_and_timers (cycle);/* process events, core Method of the event module * // * force process shutdown */If (ngx_terminate) ngx_worker_process_exit (cycle );/* Directly call the exit_process method of all modules to exit the worker process * // elegantly close the connection */If (ngx_quit) {ngx_quit = 0; ngx_setproctitle ("Worker Process is shutting down "); /* modify the process name */If (! Ngx_exiting) {ngx_close_listening_sockets (cycle);/* disable the listening handle */ngx_exiting = 1;/* ngx_exiting identifies the unique location to be modified */} If (ngx_reopen) {/* re-open all files */ngx_reopen = 0; ngx_reopen_files (cycle,-1 );}}}


The flowchart is as follows:


Master process workflow the master process continuously loops in the ngx_master_process_cycle function. It does not process network events, but only manages worker subprocesses. Similar to the worker process, the master determines the program execution flow by monitoring Seven Flags. The Seven Flags are as follows:


The following is the simplified ngx_master_process_cycle function:
Voidngx_master_process_cycle (ngx_cycle_t * cycle ){... /* Start worker_processes Work Sub-processes according to the configuration file */ngx_start_worker_processes (cycle, CCF-> worker_processes, ngx_process_respawn);/* master process */(;;) {sigsuspend (& set);/* blocking, waiting for signal */If (ngx_reap) {ngx_reap = 0;/* monitoring all sub-processes, restart the abnormal sub-process */live = ngx_reap_children (cycle);}/* live is 0, indicating that all sub-processes have exited */If (! Live & (ngx_terminate | ngx_quit) {/* exit the master process if the preceding conditions are met, including: * 1. Delete the PID file of the stored process * 2. Call the exit_master Method * Of all modules 3. Disable the listening port * 4. Destroy the memory pool */ngx_master_process_exit (cycle );} if (ngx_terminate)/* force close */{If (delay> 1000) {ngx_signal_worker_processes (cycle, sigkill ); /* sigkill = 9 */} else {/* ngx_terminate_signal = term = 15 * Send the term signal to each sub-process */cycle (cycle, ngx_signal_value (ngx_terminate_signal);} continue; /* jump up and suspend */} If (ngx_quit)/* gracefully exit, send the quit signal to all sub-Processes */{ngx_signal_worker_processes (cycle, ngx_signal_value (ngx_shutdown_signal )); /* ngx_shutdown_signal = quit */ls = Cycle-> listening. ELTs;/* close all ports */For (n = 0; n <cycle-> listening. nelts; n ++) ngx_close_socket (LS [N]. FD); cycle-> listening. nelts = 0; continue;/* Hop-up pending */} If (ngx_reconfigure)/* requires re-reading the configuration file */{/* after re-reading the configuration file, generate a new worker process and destroy the old worker process */ngx_reconfigure = 0; cycle = ngx_init_cycle (cycle);/* reconfigure the ngx_cycle_t struct */ngx_cycle = cycle; CCF = (ngx_core_conf_t *) ngx_get_conf (cycle-> conf_ctx, ngx_core_module);/* re-dispatch the child process */dispatch (cycle, CCF-> worker_processes, cycle); cycle (cycle, 1); live = 1;/* indicates that a sub-process is running * // * Send a quit signal to the old sub-process and require them to exit */ngx_signal_worker_processes (cycle, ngx_signal_value (ngx_shutdown_signal);} If (ngx_restart)/* restart the work process */{ngx_restart = 0; restart (cycle, CCF-> worker_processes, cycle); cycle (cycle, 0); live = 1;/* indicates that a sub-process is running */} If (ngx_reopen)/* re-open all files */{ngx_reopen = 0; ngx_reopen_files (cycle, CCF-> User);/* Send the usr1 signal to all sub-processes, requiring each sub-process to re-open the file */ngx_signal_worker_processes (cycle, ngx_signal_value (ngx_reopen_signal);} If) /* smooth upgrade */{ngx_change_binary = 0; ngx_new_binary = ngx_exec_new_binary (cycle, ngx_argv);/* start a new sub-process to start nginx */} If (ngx_noaccept) /* elegantly shut down the service */{ngx_noaccept = 0; ngx_noaccepting = 1;/* Send a quit signal to all sub-processes, requiring them to gracefully shut down the service */ngx_signal_worker_processes (cycle, ngx_signal_value (ngx_shutdown_signal ));}}}

The flowchart is as follows:

Reference: Understanding nginx P275-P286.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.