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

我们之前说过模块的上下文分别对应四种结构体分别是

ngx_core_module_t、

ngx_event_module_t、

ngx_http_module_t、

ngx_mail_module_t

分别对应四类模块

  1. src/core/ngx_conf_file.h  
  2. typedef struct {  
  3.     ngx_str_t             name;  
  4.     void               *(*create_conf)(ngx_cycle_t *cycle);  
  5.     char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);  
  6. } ngx_core_module_t;  
  7.   
  8.   
  9. src/event/ngx_event.h  
  10. typedef struct {  
  11.     ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  12.     ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  13.   
  14.     ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  15.     ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  16.   
  17.     ngx_int_t  (*add_conn)(ngx_connection_t *c);  
  18.     ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);  
  19.   
  20.     ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);  
  21.     ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,  
  22.                    ngx_uint_t flags);  
  23.   
  24.     ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);  
  25.     void       (*done)(ngx_cycle_t *cycle);  
  26. } ngx_event_actions_t;  
  27. typedef struct {  
  28.     ngx_str_t              *name;  
  29.   
  30.     void                 *(*create_conf)(ngx_cycle_t *cycle);  
  31.     char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);  
  32.   
  33.     ngx_event_actions_t     actions;  
  34. } ngx_event_module_t;  
  35.   
  36.   
  37.   
  38. src/http/ngx_http_config.h  
  39. typedef struct {  
  40. ngx_int_t (*preconfiguration)(ngx_conf_t *cf);  
  41. ngx_int_t (*postconfiguration)(ngx_conf_t *cf);  
  42.   
  43. void *(*create_main_conf)(ngx_conf_t *cf);  
  44. char *(*init_main_conf)(ngx_conf_t *cf, void *conf);  
  45.   
  46. void *(*create_srv_conf)(ngx_conf_t *cf);  
  47. char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);  
  48.   
  49. void *(*create_loc_conf)(ngx_conf_t *cf);  
  50. char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);  
  51. } ngx_http_module_t;   
  52.   
  53. src/mail/ngx_mail.h  
  54. typedef struct {  
  55. ngx_mail_protocol_t *protocol;  
  56.   
  57. void *(*create_main_conf)(ngx_conf_t *cf);  
  58. char *(*init_main_conf)(ngx_conf_t *cf, void *conf);  
  59.   
  60. void *(*create_srv_conf)(ngx_conf_t *cf);  
  61. char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,  
  62. void *conf);  
  63. } 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的相关数据结构。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值