Nginx的主流程的实现函数在./src/core/nginx.c文件中
其中的重点模块会在后面的文章里详解
-------------------------------------------------
大部分初始化工作的中心,变量cycle解析:
-------------------------------------------------
static ngx_uint_t ngx_show_help; //是否显示帮助信息
static ngx_uint_t ngx_show_version; //是否显示版本号
static ngx_uint_t ngx_show_configure; //是否显示配置信息
static u_char *ngx_prefix; //Nginx的工作目录
static u_char *ngx_conf_file; //全局配置文件目录地址
static u_char *ngx_conf_params; //配置参数
static char *ngx_signal; //信号
static char **ngx_os_environ;
int ngx_cdecl
main(int argc, char *const *argv)
{
ngx_int_t i;
ngx_log_t *log;
ngx_cycle_t *cycle, init_cycle;
ngx_core_conf_t *ccf;
ngx_debug_init();
if (ngx_strerror_init() != NGX_OK) //警告输出初始化
{
return 1;
}
if (ngx_get_options(argc, argv) != NGX_OK) //解析命令行中的参数
{
return 1;
}
if (ngx_show_version) {
ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);
if (ngx_show_help) {
ngx_write_stderr(
"Usage: nginx [-?hvVtq] [-s signal] [-c filename] "
"[-p prefix] [-g directives]" NGX_LINEFEED
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 directives out of configuration "
"file" NGX_LINEFEED NGX_LINEFEED
);
}
if (ngx_show_configure) {
ngx_write_stderr(
#ifdef NGX_COMPILER
"built by " NGX_COMPILER NGX_LINEFEED
#endif
#if (NGX_SSL)
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
"TLS SNI support enabled" NGX_LINEFEED
#else
"TLS SNI support disabled" NGX_LINEFEED
#endif
#endif
"configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
}
if (!ngx_test_config) {
return 0;
}
}
/* TODO */ ngx_max_sockets = -1;
ngx_time_init(); //时间更新并初始化
#if (NGX_PCRE)
ngx_regex_init();
#endif
ngx_pid = ngx_getpid(); //获取当前进程pid 并将pid存放在/usr/local/nginx-1.4.7/nginx.pid中
log = ngx_log_init(ngx_prefix); //初始化日志,log为日志文件句柄ngx_log_file.fd
if (log == NULL) {
return 1;
}
/* STUB */
#if (NGX_OPENSSL)
ngx_ssl_init(log);
#endif
/*
* init_cycle->log is required for signal handlers and
* ngx_process_options()
*/
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));//初始化init_cycle
init_cycle.log = log;
ngx_cycle = &init_cycle; //将init_cycle赋值给全局变量
init_cycle.pool = ngx_create_pool(1024, log); //为init_cycle分配1024大小的内存
if (init_cycle.pool == NULL) {
return 1;
}
if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) //保存Nginx命令行中的参数和变量,到全局变量ngx_argv
{
return 1;
}
if (ngx_process_options(&init_cycle) != NGX_OK) //将ngx_get_options中获得的参数(prefix, conf_prefix, conf_file, conf_param等字段)取值赋值到ngx_cycle中。
{
return 1;
}
if (ngx_os_init(log) != NGX_OK) //初始化系统相关变量,如内存页面大小ngx_pagesize,ngx_cacheline_size,最大连接数ngx_max_sockets等
{
return 1;
}
/*
* ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
*/
if (ngx_crc32_table_init() != NGX_OK) //初始化一致性hash表,主要作用是用状态机方式加快查询,
{
return 1;
}
if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) //ngx_add_inherited_sockets主要是继承了socket的套接字。主要作用是热启动的时候需要平滑过渡
{
return 1;
}
//前置的初始化模块,对模块进行编号处理
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}
cycle = ngx_init_cycle(&init_cycle); //完成变量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;
}
if (ngx_signal) //如果有信号,则进入ngx_signal_process方法 -s 等实现
{
return ngx_signal_process(cycle, ngx_signal);
}
ngx_os_status(cycle->log); //在日志中打印系统状态
ngx_cycle = cycle;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);//获取配置到指针ccf
if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
ngx_process = NGX_PROCESS_MASTER;
}
#if !(NGX_WIN32)
if (ngx_init_signals(cycle->log) != NGX_OK) {
return 1;
}
if (!ngx_inherited && ccf->daemon) {
if (ngx_daemon(cycle->log) != NGX_OK) {
return 1;
}
ngx_daemonized = 1;
}
if (ngx_inherited) {
ngx_daemonized = 1;
}
#endif
if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) //创建pid文件
{
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); //创建worker进程,开始监听
} else {
ngx_master_process_cycle(cycle);
}
return 0;
}