是ngx_module_s结构体的别名
typedef struct ngx_module_s ngx_module_t;
ngx_module_s结构体定义如下
- struct ngx_module_s {
- ngx_uint_t ctx_index;
- ngx_uint_t index;
-
- char *name;
-
- ngx_uint_t spare0;
- ngx_uint_t spare1;
-
- ngx_uint_t version;
- const char *signature;
-
- void *ctx;
- ngx_command_t *commands;
- ngx_uint_t type;
-
- 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;
- };
作为nginx模块的通用接口。主要方法有
成员有
ctx:void*,提供灵活性
type:模块类型
核心模块接口其定义为
- 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;
事件模块接口其定义为
- typedef struct {
- ngx_str_t *name;
-
- void *(*create_conf)(ngx_cycle_t *cycle);
- char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
-
- ngx_event_actions_t actions;
- } ngx_event_module_t;
http模块接口定义为
- typedef struct {
- ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
- ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
-
- void *(*create_main_conf)(ngx_conf_t *cf);
- char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
-
- void *(*create_srv_conf)(ngx_conf_t *cf);
- char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
-
- void *(*create_loc_conf)(ngx_conf_t *cf);
- char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
- } ngx_http_module_t;
邮件模块接口定义 为
- typedef struct {
- ngx_mail_protocol_t *protocol;
-
- void *(*create_main_conf)(ngx_conf_t *cf);
- char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
-
- void *(*create_srv_conf)(ngx_conf_t *cf);
- char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
- void *conf);
- } ngx_mail_module_t;
模块与模块子接口的关系主要是通过模块中的ctx来指定,与继承关系有些类似
主要用于在解析配置时来设置配置项,其接口定义为
- 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_uint_t conf;
- ngx_uint_t offset;
- void *post;
- };
是ngx_cycle_s结构体的别名,其结构定义如下
- struct ngx_cycle_s {
- void ****conf_ctx;
- ngx_pool_t *pool; //内存池
-
- //日志
- ngx_log_t *log;
- ngx_log_t new_log;
-
- ngx_uint_t log_use_stderr; /* unsigned log_use_stderr:1; */
-
- //连接池
- ngx_connection_t **files;
- ngx_connection_t *free_connections;
- ngx_uint_t free_connection_n;
-
- //模块数组
- ngx_module_t **modules;
- ngx_uint_t modules_n;
- ngx_uint_t modules_used; /* unsigned modules_used:1; */
-
- ngx_queue_t reusable_connections_queue;
- ngx_uint_t reusable_connections_n;
- time_t connections_reuse_time;
-
- ngx_array_t listening;//监听端口列表
- ngx_array_t paths; //保存所有的操作目录
-
- //dump配置文件
- ngx_array_t config_dump;//dump配置文件 动态数组
- ngx_rbtree_t config_dump_rbtree;
- ngx_rbtree_node_t config_dump_sentinel;
-
- ngx_list_t open_files; //打开的文件列表
- ngx_list_t shared_memory;//共享内存列表
-
- ngx_uint_t connection_n;
- ngx_uint_t files_n;
-
- ngx_connection_t *connections;//指向当前进程中所有连接对象,与connection_n
- ngx_event_t *read_events;//指向当前进程中所有读事件对象
- ngx_event_t *write_events;//指向当前进程中所有写事件对象
-
- ngx_cycle_t *old_cycle;//主要是保存启动前期的ngx_cycle_t对象
-
- //配置文件信息
- ngx_str_t conf_file;//配置文件相对于安装目录的路径名称
- ngx_str_t conf_param;//nginx处理配置文件时需要特殊处理的在命令行携带的参数
- ngx_str_t conf_prefix;//nginx配置文件所在目录的路径
- ngx_str_t prefix;//nginx安装目录的路径
- ngx_str_t error_log;
- ngx_str_t lock_file;
- ngx_str_t hostname; //主机名
- };
listening是数组ngx_array_t,其元素类型为ngx_listening_s,表示服务器监听的一个端口。其结构体定义为
- struct ngx_listening_s {
- ngx_socket_t fd;
-
- struct sockaddr *sockaddr;
- socklen_t socklen; /* size of sockaddr */
- size_t addr_text_max_len;
- ngx_str_t addr_text;
-
- int type;
-
- int backlog;
- int rcvbuf;
- int sndbuf;
- #if (NGX_HAVE_KEEPALIVE_TUNABLE)
- int keepidle;
- int keepintvl;
- int keepcnt;
- #endif
-
- /* handler of accepted connection */
- ngx_connection_handler_pt handler;
-
- void *servers; /* array of ngx_http_in_addr_t, for example */
-
- ngx_log_t log;
- ngx_log_t *logp;
-
- size_t pool_size;
- /* should be here because of the AcceptEx() preread */
- size_t post_accept_buffer_size;
-
- ngx_listening_t *previous;
- ngx_connection_t *connection;
-
- ngx_rbtree_t rbtree;
- ngx_rbtree_node_t sentinel;
-
- ngx_uint_t worker;
-
- unsigned open:1;
- unsigned remain:1;
- unsigned ignore:1;
-
- unsigned bound:1; /* already bound */
- unsigned inherited:1; /* inherited from previous process */
- unsigned nonblocking_accept:1;
- unsigned listen:1;
- unsigned nonblocking:1;
- unsigned shared:1; /* shared between threads or processes */
- unsigned addr_ntop:1;
- unsigned wildcard:1;
-
- #if (NGX_HAVE_INET6)
- unsigned ipv6only:1;
- #endif
- unsigned reuseport:1;
- unsigned add_reuseport:1;
- unsigned keepalive:2;
-
- unsigned deferred_accept:1;
- unsigned delete_deferred:1;
- unsigned add_deferred:1;
- #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
- char *accept_filter;
- #endif
- #if (NGX_HAVE_SETFIB)
- int setfib;
- #endif
-
- #if (NGX_HAVE_TCP_FASTOPEN)
- int fastopen;
- #endif
-
- };
ngx_core_module
ngx_events_module
ngx_openssl_module
ngx_http_module
ngx_mail_module
ngx_errlog_module
ngx_regex_module,
ngx_stream_module
ngx_thread_pool_module
ngx_google_perftools_module
ngx_conf_module
主要包含
其关系如下: