1.模块
1.1 ngx_module_t
是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模块的通用接口。主要方法有
- init_master
- init_module
- init_process
- init_thread
- exit_thread
- exit_process
- exit_master
成员有
ctx:void*,提供灵活性
type:模块类型
2.模块子接口
2.1 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_core_module_t;
2.2 ngx_event_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;
2.3 ngx_http_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;
2.4 ngx_mail_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来指定,与继承关系有些类似
3. ngx_command_s
主要用于在解析配置时来设置配置项,其接口定义为
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;
};
4.ngx_cycle_t
是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
};
5.五大类型模块
5.1 核心模块(NGX_CORE_MODULE)
-
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
5.2 配置模块(NGX_CONF_MODULE)
-
ngx_conf_module
5.3 事件模块(NGX_EVENT_MODULE)
- ngx_event_core_module
- ngx_devpoll_module
- ngx_epoll_module
- ngx_eventport_module
- ngx_iocp_module
- ngx_kqueue_module
- ngx_poll_module
- ngx_select_module
- ngx_poll_module(win32)
- ngx_select_module(win32)
5.4 HTTP模块(NGX_HTTP_MODULE)
- ngx_http_core_module
- ngx_http_addition_filter_module
- ngx_http_auth_basic_module
- ngx_http_auth_request_module
- ngx_http_autoindex_module
- ngx_http_browser_module
- ngx_http_charset_filter_module
- ngx_http_chunked_filter_module
- ngx_http_copy_filter_module
- ngx_http_dav_module
- ngx_http_degradation_module
- ngx_http_empty_gif_module
- ngx_http_fastcgi_module
- ngx_http_flv_module
- ngx_http_geoip_module
- ngx_http_geo_module
- ngx_http_grpc_module
- ngx_http_gunzip_filter_module
- ngx_http_gzip_filter_module
- ngx_http_gzip_static_module
- ngx_http_headers_filter_module
- ngx_http_header_filter_module
- ngx_http_image_filter_module
- ngx_http_index_module
- ngx_http_limit_conn_module
- ngx_http_limit_req_module
- ngx_http_log_module
- ngx_http_map_module
- ngx_http_memcached_module
- ngx_http_mirror_module
- ngx_http_mp4_module
- ngx_http_not_modified_filter_module
- ngx_http_perl_module
- ngx_http_postpone_filter_module
- ngx_http_proxy_module
- ngx_http_random_index_module
- ngx_http_range_header_filter_module
- ngx_http_range_body_filter_module
- ngx_http_realip_module
- ngx_http_referer_module
- ngx_http_rewrite_module
- ngx_http_scgi_module
- ngx_http_secure_link_module
- ngx_http_slice_filter_module
- ngx_http_split_clients_module
- ngx_http_ssi_filter_module
- ngx_http_ssl_module
- ngx_http_static_module
- ngx_http_stub_status_module
- ngx_http_sub_filter_module
- ngx_http_try_files_module
- ngx_http_upstream_module
- ngx_http_upstream_hash_module
- ngx_http_upstream_ip_hash_module
- ngx_http_upstream_keepalive_module
- ngx_http_upstream_least_conn_module
- ngx_http_upstream_random_module
- ngx_http_upstream_zone_module
- ngx_http_userid_filter_module
- ngx_http_uwsgi_module
- ngx_http_v2_filter_module
- ngx_http_v2_module
- ngx_http_write_filter_module
- ngx_http_xslt_filter_module
5.5 mail模块(NGX_MAIL_MODULE)
- ngx_mail_core_module
- ngx_mail_auth_http_module
- ngx_mail_imap_module
- ngx_mail_pop3_module
- ngx_mail_proxy_module
- ngx_mail_realip_module
- ngx_mail_smtp_module
- ngx_mail_ssl_module
6、事件模块
6.1 基础数据结构
主要包含
- ngx_event_actions_t:对于不同操作系统中事件管理的抽象
- ngx_event_module_t:事件模块的抽象
- ngx_event_t:描述事件数据
其关系如下: