作者:mobiledu2502861133 | 来源:互联网 | 2023-08-26 11:18
所在需要加载的模块名数组:
char *ngx_module_names[] = {"ngx_core_module","ngx_errlog_module","ngx_conf_module",…………"ngx_http_module"…………
}ngx_module_t *ngx_modules[] = {&ngx_core_module,&ngx_errlog_module,&ngx_conf_module,&ngx_regex_module,&ngx_events_module,&ngx_event_core_module,&ngx_epoll_module,&ngx_thread_pool_module,&ngx_http_module,&ngx_http_core_module,&ngx_http_log_module,&ngx_http_upstream_module,&ngx_http_static_module,&ngx_http_autoindex_module,&ngx_http_index_module,&ngx_http_mirror_module,&ngx_http_try_files_module,&ngx_http_auth_basic_module,&ngx_http_access_module,&ngx_http_limit_conn_module,&ngx_http_limit_req_module,&ngx_http_geo_module,&ngx_http_map_module,&ngx_http_split_clients_module,&ngx_http_referer_module,&ngx_http_rewrite_module,&ngx_http_proxy_module,&ngx_http_fastcgi_module,&ngx_http_uwsgi_module,&ngx_http_scgi_module,&ngx_http_memcached_module,&ngx_http_empty_gif_module,&ngx_http_browser_module,&ngx_http_upstream_hash_module,&ngx_http_upstream_ip_hash_module,&ngx_http_upstream_least_conn_module,&ngx_http_upstream_random_module,&ngx_http_upstream_keepalive_module,&ngx_http_upstream_zone_module,&ngx_http_write_filter_module,&ngx_http_header_filter_module,&ngx_http_chunked_filter_module,&ngx_http_range_header_filter_module,&ngx_http_gzip_filter_module,&ngx_http_postpone_filter_module,&ngx_http_ssi_filter_module,&ngx_http_charset_filter_module,&ngx_http_userid_filter_module,&ngx_http_headers_filter_module,&ngx_http_copy_filter_module,&ngx_http_range_body_filter_module,&ngx_http_not_modified_filter_module,NULL
};//http 模块结构体
//NGX_MODULE_V1 宏定义包含了name字段,是一个空指针,ngx_preinit_modules函数会将ngx_module_names中的字符串赋值给它
//系统初始化时解析配置,解析到ngx_http_commands中对应的指令,会调ngx_http_commands中的set回调函数
ngx_module_t ngx_http_module = {NGX_MODULE_V1,&ngx_http_module_ctx, /* module context */ngx_http_commands, /* module directives */NGX_CORE_MODULE, /* module type */NULL, /* init master */NULL, /* init module */NULL, /* init process */NULL, /* init thread */NULL, /* exit thread */NULL, /* exit process */NULL, /* exit master */NGX_MODULE_V1_PADDING
};//nginx启动时解析配置,解析到"http"时,会调用ngx_http_block
static ngx_command_t ngx_http_commands[] = {{ ngx_string("http"),NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,ngx_http_block,0,0,NULL },ngx_null_command
};
以HTTP模块为例分析:
main
|
|__ngx_preinit_modules //用ngx_module_names列出的模块名,初始化ngx_modules[]结构体数组
|
|__ngx_init_cycle||__ngx_cycle_modules //将ngx_modules[]拷贝到cycle->modules||__ngx_conf_parse| || |__ngx_conf_handler| || |__ngx_http_block//HTTP 模块的入口函数||__ngx_init_modules//调用 ngx_module_t 结构中的init_module回调函数