/** regex模块分配空间,并设为默认值 */ static void * ngx_regex_create_conf(ngx_cycle_t *cycle) { ngx_regex_conf_t *rcf; rcf = ngx_pcalloc(cycle->pool, sizeof(ngx_regex_conf_t)); if (rcf NULL) { return NULL; } rcf->pcre_jit = NGX_CONF_UNSET; ngx_pcre_studies = ngx_list_create(cycle->pool, 8, sizeof(ngx_regex_elt_t)); if (ngx_pcre_studies NULL) { return NULL; } return rcf; }
读取配置文件,经过command将须要的初始化和设置二级模块建立,若是没有碰见一级模块指令。nginx
检测NGX_CORE_MODULE模块的init_conf()来将上下文配置中没有设置参数,设置为默认值redis
完成配置模块上下文配置的初始化,在ngx_worker_process_init() 中调用模块的 init_process() 启动上面初始化的模块(不过在源码中只有 nginx_event_core_module有这个启动函数ngx_event_process_init(),其它模块都是NULL)json
/** 初始化二级模块(NGX_EVENT_MODULE类型)ngx_event_core_module模块的上下文配置 */ static void * ngx_event_core_create_conf(ngx_cycle_t *cycle) { ngx_event_conf_t *ecf; ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t)); if (ecf NULL) { return NULL; } ecf->connections = NGX_CONF_UNSET_UINT; ecf->use = NGX_CONF_UNSET_UINT; ecf->multi_accept = NGX_CONF_UNSET; ecf->accept_mutex = NGX_CONF_UNSET; ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC; ecf->name = (void *) NGX_CONF_UNSET; return ecf; }
/*** 根据配置的信息来启用(NGX_EVENT_MODULE类型)ngx_event_core_module剩下的上下文配置信息,并将其设置到cycle上 */ static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; int fd; ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; '根据编译的时候,启动了epoll、select、etc其中一种' #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); if (fd != -1) { (void) close(fd); module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { module = &ngx_epoll_module; } #endif #if (NGX_HAVE_DEVPOLL) && !(NGX_TEST_BUILD_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif #if (NGX_HAVE_SELECT) if (module NULL) { module = &ngx_select_module; } #endif // 全部的 ngx_epoll_module 模块 if (module NULL) { for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = cycle->modules[i]->ctx; if (ngx_strcmp(event_module->name->data, event_core_name.data) 0) { continue; } module = cycle->modules[i]; break; } } if (module NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); cycle->connection_n = ecf->connections; ngx_conf_init_uint_value(ecf->use, module->ctx_index); event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 0); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); return NGX_CONF_OK; }
/** 初始化二级模块(NGX_EVENT_MODULE类型)ngx_epoll_module的上下文配置 */ static void * ngx_epoll_create_conf(ngx_cycle_t *cycle) { ngx_epoll_conf_t *epcf; epcf = ngx_palloc(cycle->pool, sizeof(ngx_epoll_conf_t)); if (epcf NULL) { return NULL; } epcf->events = NGX_CONF_UNSET; epcf->aio_requests = NGX_CONF_UNSET; return epcf; }
/** 设置二级模块(NGX_EVENT_MODULE类型)ngx_epoll_module的上下文配置 */ static char * ngx_epoll_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_epoll_conf_t *epcf = conf; ngx_conf_init_uint_value(epcf->events, 512); ngx_conf_init_uint_value(epcf->aio_requests, 32); return NGX_CONF_OK; }
struct ngx_module_s { ngx_uint_t ctx_index; '模块初始化内存的回调函数的索引,ngx_conf_s.ctx中的位置' ngx_uint_t index; '模块初始化内存的回调函数的索引,ngx_conf_s.ctx中的位置' char *name; //模块名称 ngx_uint_t spare0; ngx_uint_t spare1; ngx_uint_t version; //模块版本 const char *signature; 'core模块的上下文:名称,模块和命令基本信息的建立、初始化回调函数' void *ctx; '指令上下文:名称基本信息的初始化回调函数' ngx_command_t *commands; ngx_uint_t type; //该模块实例的类型标识core,http,event和mail, ngx_int_t (*init_master)(ngx_log_t *log); //主进程初始化时调用 ngx_int_t (*init_module)(ngx_cycle_t *cycle); //模块初始化时调用 ngx_int_t (*init_process)(ngx_cycle_t *cycle); //工做进程初始化时调用 ngx_int_t (*init_thread)(ngx_cycle_t *cycle); //线程初始化时调用 void (*exit_thread)(ngx_cycle_t *cycle); //线程退出时调用 void (*exit_process)(ngx_cycle_t *cycle); //工做进程退出时调用 void (*exit_master)(ngx_cycle_t *cycle); //主进程退出时调用 //如下是预留成员 uintptr_t spare_hook0; uintptr_t spare_hook1; uintptr_t spare_hook2; uintptr_t spare_hook3; uintptr_t spare_hook4; uintptr_t spare_hook5; uintptr_t spare_hook6; uintptr_t spare_hook7; };
模块类型类型 | 模块名称 | 指令 |
---|---|---|
NGX_CORE_MODULE | errlog | error_log |
NGX_CORE_MODULE | thread_pool | thread_pool |
NGX_CORE_MODULE | openssl | ssl_engine |
NGX_CORE_MODULE | http | http |
NGX_CORE_MODULE | ||
NGX_CORE_MODULE | google_perftools | google_perftools_profiles |
NGX_CORE_MODULE | stream | stream |
NGX_CORE_MODULE | core | daemon、master_process、timer_resolution、pid、lock_file、worker_processes、debug_points、user、worker_priority、worker_cpu_affinity、worker_rlimit_nofile、worker_rlimit_core、worker_shutdown_timeout、working_directory、env、load_module |
NGX_CONF_MODULE | NULL | include |
typedef struct { ngx_str_t name; void *(*create_conf)(ngx_cycle_t *cycle); char *(*init_conf)(ngx_cycle_t *cycle, void *conf); } ngx_core_module_t;
struct ngx_command_s { ngx_str_t name; //指令名称 ngx_uint_t type; // 指令类型 //配置文件的指令解析函数 char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); //该字段被NGX_HTTP_MODULE类型模块所用 (咱们编写的基本上都是NGX_HTTP_MOUDLE,只有一些nginx核心模块是非NGX_HTTP_MODULE),该字段指定当前配置项存储的内存位置。 //其实是使用哪一个内存池的问题。其实是使用哪一个内存池的问题。由于http模块对全部http模块所要保存的配置信息,划分了main, server和location三个地方进行存储,每一个地方都有一个内存池用来分配存储这些信息的内存。 //这里可能的值为 NGX_HTTP_MAIN_CONF_OFFSET、NGX_HTTP_SRV_CONF_OFFSET或NGX_HTTP_LOC_CONF_OFFSET。固然也能够直接置为0,就是NGX_HTTP_MAIN_CONF_OFFSET。 ngx_uint_t conf; //指定该配置项值的精确存放位置,通常指定为某一个结构体变量的字段偏移。 //由于对于配置信息的存储,通常咱们都是定义个结构体来存储的。那么好比咱们定义了一个结构体A,该项配置的值须要存储到该结构体的b字段。 // 那么在这里就能够填写为offsetof(A, b)。对于有些配置项,它的值不须要保存或者是须要保存到更为复杂的结构中时,这里能够设置为0 ngx_uint_t offset; void *post; //该字段存储一个指针。能够指向任何一个在读取配置过程当中须要的数据,以便于进行配置读取的处理。大多数时候,都不须要,因此简单地设为0便可。 };
指令类型 | 说明 |
---|---|
NGX_MAIN_CONF | 例如:http、mail、events、error_log等 |
NGX_DIRECT_CONF | 能够出如今配置文件中最外层。例如已经提供的配置指令daemon,master_process等 |
NGX_ANY_CONF | 该配置指令能够出如今任意配置级别上 |
NGX_MAIN_CONF | http、mail、events、error_log等 |
NGX_CONF_BLOCK | 配置指令能够接受的值是一个配置信息块。也就是一对大括号括起来的内容。里面能够再包括不少的配置指令。好比常见的server指令就是这个属性的 |
NGX_CONF_FLAG | 配置指令能够接受的值是”on”或者”off”,最终会被转成bool值 |
NGX_CONF_ANY | 配置指令能够接受的任意的参数值。一个或者多个,或者”on”或者”off”,或者是配置块 |
NGX_CONF_ANY | 配置指令能够接受的任意的参数值。一个或者多个,或者”on”或者”off”,或者是配置块 |
HTTP模块 | |
NGX_HTTP_MAIN_CONF | 能够直接出如今http配置指令里 |
NGX_HTTP_SRV_CONF | 能够出如今http里面的server配置指令里 |
NGX_HTTP_LOC_CONF | 能够出如今http server块里面的location配置指令里 |
NGX_HTTP_UPS_CONF | 能够出如今http里面的upstream配置指令里 |
NGX_HTTP_SIF_CONF | 能够出如今http里面的server配置指令里的if语句所在的block中 |
NGX_HTTP_LMT_CONF | 能够出如今http里面的limit_except指令的block中 |
NGX_HTTP_LMT_CONF | 能够出如今http里面的limit_except指令的block中 |
NGX_HTTP_LMT_CONF | 能够出如今http里面的limit_except指令的block中 |
NGX_HTTP_LIF_CONF | 能够出如今http server块里面的location配置指令里的if语句所在的block中 |
nginx的配置指令的参数个数不能够超过NGX_CONF_MAX_ARGS个,目前这个值被定义为8,也就是不能超过8个参数值 | |
NGX_CONF_NOARGS | 配置指令不接受任何参数 |
NGX_CONF_TAKE1 | 配置指令接受1个参数 |
NGX_CONF_TAKE2 | 配置指令接受2个参数 |
NGX_CONF_TAKE3 | 配置指令接受3个参数 |
NGX_CONF_TAKE4 | 配置指令接受4个参数 |
NGX_CONF_TAKE5 | 配置指令接受5个参数 |
NGX_CONF_TAKE6 | 配置指令接受6个参数 |
NGX_CONF_TAKE8 | 配置指令接受7个参数 |
NGX_CONF_TAKE12 | 配置指令接受1个或者2个参数 |
NGX_CONF_TAKE13 | 配置指令接受1个或者3个参数 |
NGX_CONF_TAKE23 | 配置指令接受2个或者3个参数 |
NGX_CONF_TAKE123 | 配置指令接受1个或者2个或者3参数 |
NGX_CONF_TAKE1234 | 配置指令接受1个或者2个或者3个或者4个参数 |
NGX_CONF_1MORE | 配置指令接受至少一个参数 |
NGX_CONF_2MORE | 配置指令接受至少两个参数 |
NGX_CONF_MULTI | 配置指令能够接受多个参数,即个数不定。 |
NGX_CONF_2MORE | 配置指令接受至少两个参数 |
NGX_CONF_2MORE | 配置指令接受至少两个参数 |
ngx_module_t ngx_core_module = { NGX_MODULE_V1, &ngx_core_module_ctx, /* module context */ ngx_core_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ //NGX_CORE_MODULE 0x45524F43 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 }; // NGX_MODULE_V1 #define NGX_MODULE_V1 NGX_MODULE_UNSET_INDEX, // ngx_module_s.ctx_index = NGX_MODULE_UNSET_INDEX (ngx_uint_t) -1 NGX_MODULE_UNSET_INDEX, // ngx_module_s.index = NGX_MODULE_UNSET_INDEX (ngx_uint_t) -1 NULL, // ngx_module_s.name 0, // ngx_module_s.spare0 0, // ngx_module_s.spare1 nginx_version, // ngx_module_s.nginx_version NGX_MODULE_SIGNATURE // ngx_module_s.signature // 上个信号:NGX_MODULE_SIGNATURE #define NGX_MODULE_SIGNATURE \ NGX_MODULE_SIGNATURE_0 NGX_MODULE_SIGNATURE_1 NGX_MODULE_SIGNATURE_2 \ NGX_MODULE_SIGNATURE_3 NGX_MODULE_SIGNATURE_4 NGX_MODULE_SIGNATURE_5 \ NGX_MODULE_SIGNATURE_6 NGX_MODULE_SIGNATURE_7 NGX_MODULE_SIGNATURE_8 \ NGX_MODULE_SIGNATURE_9 NGX_MODULE_SIGNATURE_10 NGX_MODULE_SIGNATURE_11 \ NGX_MODULE_SIGNATURE_12 NGX_MODULE_SIGNATURE_13 NGX_MODULE_SIGNATURE_14 \ NGX_MODULE_SIGNATURE_15 NGX_MODULE_SIGNATURE_16 NGX_MODULE_SIGNATURE_17 \ NGX_MODULE_SIGNATURE_18 NGX_MODULE_SIGNATURE_19 NGX_MODULE_SIGNATURE_20 \ NGX_MODULE_SIGNATURE_21 NGX_MODULE_SIGNATURE_22 NGX_MODULE_SIGNATURE_23 \ NGX_MODULE_SIGNATURE_24 NGX_MODULE_SIGNATURE_25 NGX_MODULE_SIGNATURE_26 \ NGX_MODULE_SIGNATURE_27 NGX_MODULE_SIGNATURE_28 NGX_MODULE_SIGNATURE_29 \ NGX_MODULE_SIGNATURE_30 NGX_MODULE_SIGNATURE_31 NGX_MODULE_SIGNATURE_32 \ NGX_MODULE_SIGNATURE_33 NGX_MODULE_SIGNATURE_34 // NGX_MODULE_V1_PADDING #define NGX_MODULE_V1_PADDING 0, // ngx_module_s.spare_hook0 0, // ngx_module_s.spare_hook1 0, // ngx_module_s.spare_hook2 0, // ngx_module_s.spare_hook3 0, // ngx_module_s.spare_hook4 0, // ngx_module_s.spare_hook5 0, // ngx_module_s.spare_hook6 0 // ngx_module_s.spare_hook7
#include <ngx_config.h> #include <ngx_core.h> extern ngx_module_t ngx_core_module; extern ngx_module_t ngx_errlog_module; extern ngx_module_t ngx_conf_module; extern ngx_module_t ngx_openssl_module; extern ngx_module_t ngx_regex_module; extern ngx_module_t ngx_events_module; extern ngx_module_t ngx_event_core_module; extern ngx_module_t ngx_epoll_module; extern ngx_module_t ngx_http_module; extern ngx_module_t ngx_http_core_module; extern ngx_module_t ngx_http_log_module; extern ngx_module_t ngx_http_upstream_module; extern ngx_module_t ngx_http_static_module; extern ngx_module_t ngx_http_autoindex_module; extern ngx_module_t ngx_http_index_module; extern ngx_module_t ngx_http_auth_basic_module; extern ngx_module_t ngx_http_access_module; extern ngx_module_t ngx_http_limit_conn_module; extern ngx_module_t ngx_http_limit_req_module; extern ngx_module_t ngx_http_geo_module; extern ngx_module_t ngx_http_map_module; extern ngx_module_t ngx_http_split_clients_module; extern ngx_module_t ngx_http_referer_module; extern ngx_module_t ngx_http_rewrite_module; extern ngx_module_t ngx_http_ssl_module; extern ngx_module_t ngx_http_proxy_module; extern ngx_module_t ngx_http_fastcgi_module; extern ngx_module_t ngx_http_uwsgi_module; extern ngx_module_t ngx_http_scgi_module; extern ngx_module_t ngx_http_memcached_module; extern ngx_module_t ngx_http_empty_gif_module; extern ngx_module_t ngx_http_browser_module; extern ngx_module_t ngx_http_upstream_hash_module; extern ngx_module_t ngx_http_upstream_ip_hash_module; extern ngx_module_t ngx_http_upstream_least_conn_module; extern ngx_module_t ngx_http_upstream_keepalive_module; extern ngx_module_t ngx_http_upstream_zone_module; extern ngx_module_t ndk_http_module; extern ngx_module_t ngx_coolkit_module; extern ngx_module_t ngx_http_set_misc_module; extern ngx_module_t ngx_http_form_input_module; extern ngx_module_t ngx_http_encrypted_session_module; extern ngx_module_t ngx_http_lua_upstream_module; extern ngx_module_t ngx_http_array_var_module; extern ngx_module_t ngx_http_memc_module; extern ngx_module_t ngx_http_redis2_module; extern ngx_module_t ngx_http_redis_module; extern ngx_module_t ngx_http_write_filter_module; extern ngx_module_t ngx_http_header_filter_module; extern ngx_module_t ngx_http_chunked_filter_module; extern ngx_module_t ngx_http_range_header_filter_module; extern ngx_module_t ngx_http_gzip_filter_module; extern ngx_module_t ngx_http_postpone_filter_module; extern ngx_module_t ngx_http_ssi_filter_module; extern ngx_module_t ngx_http_charset_filter_module; extern ngx_module_t ngx_http_userid_filter_module; extern ngx_module_t ngx_http_headers_filter_module; extern ngx_module_t ngx_http_echo_module; extern ngx_module_t ngx_http_xss_filter_module; extern ngx_module_t ngx_http_srcache_filter_module; extern ngx_module_t ngx_http_lua_module; extern ngx_module_t ngx_http_headers_more_filter_module; extern ngx_module_t ngx_http_rds_json_filter_module; extern ngx_module_t ngx_http_rds_csv_filter_module; extern ngx_module_t ngx_http_copy_filter_module; extern ngx_module_t ngx_http_range_body_filter_module; extern ngx_module_t ngx_http_not_modified_filter_module; ngx_module_t *ngx_modules[] = { &ngx_core_module, &ngx_errlog_module, &ngx_conf_module, &ngx_openssl_module, &ngx_regex_module, &ngx_events_module, &ngx_event_core_module, &ngx_epoll_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_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_ssl_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_keepalive_module, &ngx_http_upstream_zone_module, &ndk_http_module, &ngx_coolkit_module, &ngx_http_set_misc_module, &ngx_http_form_input_module, &ngx_http_encrypted_session_module, &ngx_http_lua_upstream_module, &ngx_http_array_var_module, &ngx_http_memc_module, &ngx_http_redis2_module, &ngx_http_redis_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_echo_module, &ngx_http_xss_filter_module, &ngx_http_srcache_filter_module, &ngx_http_lua_module, &ngx_http_headers_more_filter_module, &ngx_http_rds_json_filter_module, &ngx_http_rds_csv_filter_module, &ngx_http_copy_filter_module, &ngx_http_range_body_filter_module, &ngx_http_not_modified_filter_module, NULL }; char *ngx_module_names[] = { "ngx_core_module", "ngx_errlog_module", "ngx_conf_module", "ngx_openssl_module", "ngx_regex_module", "ngx_events_module", "ngx_event_core_module", "ngx_epoll_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_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_ssl_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_keepalive_module", "ngx_http_upstream_zone_module", "ndk_http_module", "ngx_coolkit_module", "ngx_http_set_misc_module", "ngx_http_form_input_module", "ngx_http_encrypted_session_module", "ngx_http_lua_upstream_module", "ngx_http_array_var_module", "ngx_http_memc_module", "ngx_http_redis2_module", "ngx_http_redis_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_echo_module", "ngx_http_xss_filter_module", "ngx_http_srcache_filter_module", "ngx_http_lua_module", "ngx_http_headers_more_filter_module", "ngx_http_rds_json_filter_module", "ngx_http_rds_csv_filter_module", "ngx_http_copy_filter_module", "ngx_http_range_body_filter_module", "ngx_http_not_modified_filter_module", NULL };
这还有问题数组
//加载模块的基本信息,解析配置文件时初始化 if (ngx_cycle_modules(cycle) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_CORE_MODULE) { continue; } module = cycle->modules[i]->ctx; if (module->create_conf) { rv = module->create_conf(cycle); if (rv NULL) { ngx_destroy_pool(pool); return NULL; } cycle->conf_ctx[cycle->modules[i]->index] = rv; } }