您的位置:首页 > 运维架构 > Nginx

nginx 源码学习笔记(五)——nginx精粹-模块

2012-03-15 11:20 615 查看
我们之前说过模块的上下文分别对应四种结构体分别是

ngx_core_module_t、

ngx_event_module_t、

ngx_http_module_t、

ngx_mail_module_t

分别对应四类模块

src/core/ngx_conf_file.h
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;

src/event/ngx_event.h
typedef struct {
    ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
    ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

    ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
    ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

    ngx_int_t  (*add_conn)(ngx_connection_t *c);
    ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);

    ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
    ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
                   ngx_uint_t flags);

    ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
    void       (*done)(ngx_cycle_t *cycle);
} ngx_event_actions_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;

src/http/ngx_http_config.h
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; 

src/mail/ngx_mail.h
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;


这四个结构体都提供了钩子,以便模块注册与上下文有关的操作,核心模块提供了core和event模块也提供了create_conf和init_conf两个钩子,除此之外还提供了一些操作时间的钩子;http模块提供了几个在读配置文件前后和操作mail/src/loc配置项时执行的钩子;mail模块也提供一些类似于http模块的钩子,不过比http模块简单些。



core模块,他会在系统的许多部分配置用,比如errlog模块负责写错误日志;

event模块是时间驱动相关的模块,nginx在不同操作系统上会使用不同的内核机制,例如linux中就可以使用epoll模块;

http模块式用于处理http输入,产生输出,过滤输出,负载均衡等的模块,这是nginx作为web服务器的核心部分;

mail模块实现邮件代理的模块,实现smtp/pop3/imap等协议的邮件代理。



讲到这里希望大家能够对nginx有个大体上的认识,起码在模块上能够,其实现在可以证实开始源码精读了,但是为了更好地理解细节,我们在读源代码前要学一下nginx的相关数据结构。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: