The nginx startup process closely revolves around the ngx_cycle_t struct. First, obtain the command line parameters through ngx_get_options (), and then initialize the time through ngx_time_init (), such as the global variable ngx_cached_time; then, initialize the regular expression through ngx_regx_init (), create a log object through ngx_log_init (), initialize the log, for example, initialize the global variable ngx_prefix, open the log file ngx_log_file.fd, and create a memory pool through ngx_create_pool, the value is 1024b. Then () saves the command line parameters to the global variables, including prepare, ngx_argc, and ngx_argv. You can use ngx_process_options () to process the configuration file path in init_cycle, and initialize the prefix, conf_prefix, conf_param and other fields; ngx_ OS _init () obtains information about the operating system, such as the memory page size and system resource limit, and saves it in the corresponding global variables, initialize a cyclic redundancy check table through ngx_crc32_table_init () to prepare for efficient searching for cyclic redundancy. Use ngx_add_inherited_sockets to pass environment variables.
- Parse the sockets environment variable nginx_var = "nginx" and save it to the ngx_cycle.listening array;
- Set ngx_inherited = 1;
- Call ngx_set_inherited_sockets () to set sockets in the ngx_cycle.listening array one by one;
Then, the modules in the module array are numbered, and then ngx_init_cycle () is called to initialize the ngx_cycle_t variable cycle. The tasks in ngx_init_t are as follows:
Then, call ngx_signal_process () according to the conditions to process the process signal, obtain the operating system status information through ngx_ OS _status (), read the information of the configuration file,
- If a signal exists, the ngx_signal_process () process is enabled;
- Call ngx_init_signals () to initialize the signal. It registers the signal processing program;
- If sockets is not inherited and the daemon ID is set, call ngx_daemon () to create the daemon;
- Call ngx_create_pidfile () to create a process record file. (non-ngx_process_master = 1 process. Do not create this file)
- Enter the main process loop;
- If ngx_process_single = 1, ngx_single_process_cycle () is called to enter the process loop;
- Otherwise, in master-worker mode, call ngx_master_process_cycle () to enter the process loop;
- For more information, see <nginx source code analysis-master/Worker Process startup>;
Reference: http://blog.csdn.net/livelylittlefish/article/details/7243718
Nginx Startup Process Analysis