Today, I spent a day watching the nginx startup process. The process is still very complicated. There are more than a dozen main function calls. I learned some functions by reading the source code and checking the information on the Internet, some functions are still being learned, and some functions are still to be learned. Here we will record what we have learned today. Come on!
Http://blog.csdn.net/xiaoliangsky/article/details/39856803
1nginx. c
The started program is mainly in src/CORE/nginx. C. Like a common function, the main function is its entry function. Let's look at the source code of the main function:
int ngx_cdeclmain (INT argc, char * const * argv) {ngx_int_t I; ngx_log_t * log; ngx_cycle_t * cycle, init_cycle; # If (ngx_freebsd) ngx_debug_init (); # endif // The function is defined in the src/OS/Unix/ngx_errno.c file. The initialization error code if (ngx_strerror_init ()! = Ngx_ OK) {return 1;} If (ngx_get_options (argc, argv )! = Ngx_ OK) {// parse the nginx startup parameter return 1;} If (ngx_show_version) {ngx_write_stderr ("nginx version:" nginx_ver ngx_linefeed); If (ngx_show_help) {// start the configuration parameter ngx_write_stderr ("Usage: nginx [-? Hvvtq] [-S Signal] [-C filename] "[-P prefix] [-G directives]" ngx_linefeed "Options:" ngx_linefeed "-?, -H: This help "ngx_linefeed"-V: Show version and exit "ngx_linefeed"-V: Show version and configure options then exit "ngx_linefeed"-T: test Configuration and exit "ngx_linefeed"-Q: suppress non-error messages "" during configuration testing "ngx_linefeed"-S signal: Send signal to a master process: "" Stop, quit, reopen, reload "ngx_linefeed # ifdef ngx_prefix"-P Prefix: Set prefix PATH (default: "ngx_prefix") "ngx_linefeed # else"-P Prefix: Set prefix path (default: None) "ngx_linefeed # endif"-C filename: set configuration file (default: "ngx_conf_path") "ngx_linefeed"-G directives: set global direves ves out of configuration "" file "ngx_linefeed);} If (ngx_show_configure) {ngx_write_stderr (# ifdef ngx_compiler "built by" ngx_compiler ngx_linefeed # Endif # If (ngx_ssl) # ifdef require "tls sni support enabled" ngx_linefeed # else "tls sni support disabled" ngx_linefeed # endif "Configure arguments:" ngx_configure ngx_linefeed );} if (! Ngx_test_config) {return 0 ;}/ * todo */ngx_max_sockets =-1; ngx_time_init (); // (Core/ngx_time.h) initialization time and update system time # If (ngx_pcre) ngx_regex_init (); // src/CORE/ngx_regex.c this function is not introduced for the moment # endif ngx_pid = ngx_getpid (); // obtain the current process ID log = ngx_log_init (ngx_prefix ); // initialize the log. log is the core module and will be used to learn if (log = NULL) {return 1;}/* stub */# If (ngx_openssl) ngx_ssl_init (log ); // src/event/ngx_event_openssl.c. This function is not introduced for the moment. The event Module # End If/** init_cycle-> log is required for signal handlers and * ngx_process_options () */ngx_memzero (& init_cycle, sizeof (cycle); cycle = log; ngx_cycle = & init_cycle; init_cycle.pool = ngx_create_pool (1024, log); // create a memory pool if (init_cycle.pool = NULL) {return 1;} If (ngx_save_argv (& init_cycle, argc, argv )! = Ngx_ OK) {// Save the Configuration Parameter return 1;} // use old_cycle (init_cycle) to save some configuration information first, then this init_cycle will be used as the old_cycle if (ngx_process_options (& init_cycle) in the // ngx_init_cycle parameter )! = Ngx_ OK) {return 1;} // call ngx_ OS _init () to initialize system variables, such as the memory page size ngx_pagesize, ngx_cacheline_size, // The maximum number of connections ngx_max_sockets, And if (ngx_ OS _init (log )! = Ngx_ OK) {return 1;}/** ngx_crc32_table_init () requires ngx_cacheline_size set in ngx_ OS _init () * // call ngx_crc32_table_init () initialize the CRC table (subsequent CRC verification is performed through the Table query, which is efficient) if (ngx_crc32_table_init ()! = Ngx_ OK) {return 1;} // continue sockets and store the related information in init_cycle if (ngx_add_inherited_sockets (& init_cycle )! = Ngx_ OK) {return 1 ;}// initialize the index ngx_max_module of each module = 0; for (I = 0; ngx_modules [I]; I ++) {ngx_modules [I]-> Index = ngx_max_module ++;} // call ngx_init_cycle. This function is very important. Most initialization operations are in this function, cycle = ngx_init_cycle (& init_cycle); If (cycle = NULL) {If (ngx_test_config) {ngx_log_stderr (0, "configuration file % s test failed ", init_cycle.conf_file.data);} return 1;} If (ngx_test_config) {If (! Ngx_quiet_mode) {ngx_log_stderr (0, "configuration file % s test is successful", cycle-> conf_file.data);} return 0;} // process signal if (ngx_signal) {return ngx_signal_process (cycle, ngx_signal);} ngx_ OS _status (cycle-> log); ngx_cycle = cycle; // creat_conf for CCF creation and init_conf for initialization are CCF = (ngx_core_conf_t *) ngx_get_conf (cycle-> conf_ctx, ngx_core_module) called in the ngx_init_cycle function ); if (CCF-> master & ngx_process = = Ngx_process_single) {ngx_process = ngx_process_master;} # If! (Ngx_win32) // call the ngx_init_signals () initialization signal; mainly complete the registration of the signal processing program // This function is defined in SORC/OS/Unix/ngx_process.c if (ngx_init_signals (cycle-> log )! = Ngx_ OK) {return 1;} // If sockets is not inherited and the daemon ID is set, call ngx_daemon () to create the daemon if (! Ngx_inherited & CCF-> Daemon) {If (ngx_daemon (cycle-> log )! = Ngx_ OK) {return 1;} ngx_daemonized = 1;} If (ngx_inherited) {ngx_daemonized = 1;} # endif // call ngx_create_pidfile () to create a process record file, record process ID if (ngx_create_pidfile (& CCF-> PID, cycle-> log )! = Ngx_ OK) {return 1;} If (cycle-> log-> file-> FD! = Ngx_stderr) {If (ngx_set_stderr (cycle-> log-> file-> FD) = ngx_file_error) {ngx_log_error (ngx_log_emerg, cycle-> log, ngx_errno, ngx_set_stderr_n "failed"); return 1 ;}}if (log-> file-> FD! = Ngx_stderr) {If (ngx_close_file (log-> file-> FD) = ngx_file_error) {ngx_log_error (ngx_log_alert, cycle-> log, ngx_errno, ngx_close_file_n "Built-in log failed") ;}} ngx_use_stderr = 0; // If (ngx_process = ngx_process_single) {ngx_single_process_cycle (cycle );} else {// master process ngx_master_process_cycle (cycle) ;}return 0 ;}
Here, the function call is summarized as follows:
1 ngx_strerror_init (), which is defined in the src/OS/Unix/ngx_errno.c file. This function is mainly used to initialize the system.
The meaning of the error number. In this way, the corresponding advantage during initialization is that when an error occurs, you do not need to call the strerror () function to obtain it.
You can find the cause of the error based on the error number to improve the running efficiency;
2 ngx_get_options () Parse nginx startup parameters;
3 ngx_time_init () The function is defined in the src/CORE/ngx_time.c file. Ngx_cached_err_log_time,
Ngx_cached_http_time, ngx_cached_http_log_time, ngx_cached_http_log_iso8601 are
Ngx_str_t type, used to record the error log time, HTTP cache time, HTTP cache log time, And iso8061 time. During Initialization
First, calculate the length of the string represented by this time, which can save time for calculation. Ngx_cached_time is the nginx time class.
It is volatile-type, that is, to prevent Compiler Optimization. Each time it is read from the memory, rather than using the cache value. This function
The ngx_time_update () function is called and defined in the src/CORE/ngx_time.c file. The function is used to update the system time;
4 ngx_regex_init () src/CORE/ngx_regex.c. If the PCRE function is enabled, initialize the regular expression. Main PCRE in nginx
Is used to support URL rewrite. url rewrite is mainly used to rewrite the requested URL address in proxy mode.
Targeted access. This function is not described much now. We will introduce the HTTP module later;
5 ngx_log_init () src/CORE/ngx_log.c. initialize log, including creating log files. The log module is also the core module and will be learned later.
6 ngx_ssl_init () src/event/ngx_event_openssl.c. This function is not introduced at the moment. The event module;
7 ngx_save_argv () Save startup parameters in the ngx_argv array, and the number of parameters is stored in ngx_argc;
8 ngx_process_options () Use old_cycle to first save some configuration information: prefix, conf_prefix, conf_file, conf_param;
Then this old_cycle will be used as the old_cycle in the ngx_init_cycle parameter;
9 ngx_ OS _init () initialize system-related variables, such as memory page size ngx_pagesize, ngx_cacheline_size, and maximum number of connections ngx_max_sockets
10 ngx_crc32_table_init () calls to initialize the CRC table. Subsequent CRC checks are performed through the table, which is highly efficient;
11 ngx_add_inherited_sockets () inherits sockets and stores related information in init_cycle;
12 initialize index http://blog.csdn.net/xiaoliangsky/article/details/39856803 for each module
13 ngx_init_cycle () calls ngx_init_cycle. This function is very important. Most initialization operations are in this function, which will be detailed later.
14 if there is a signal, call ngx_siganl_process to process the signal.
15 ngx_init_signal initialize the signal. It mainly registers the signal processing program.
16 if sockets is not inherited and the daemon ID is set, call ngx_daemon () to create the daemon.
17 ngx_create_pidfile call ngx_create_pidfile () to create a process record file and record the process ID
18. Go to the Process Loop. If ngx_process = 1, go to ngx_single_process_cycle.
Ngx_process! = 1, then go to ngx_master_process_cycle
The ngx_signale_process_cycle and ngx_master_process_cycle functions will be studied later.
References:
Http://blog.csdn.net/livelylittlefish/article/details/7243718
Nginx 11 nginx Startup Process