NGINX(五)模块

nginx模块分为以下几类:

NGX_CORE_MODULE  //核心模块
NGX_HTTP_MODULE  //HTTP处理模块
NGX_EVENT_MODULE //事件处理模块
NGX_MAIL_MODULE  //邮件处理模块

涉及数据结构

/*模块可解析的配置命令*/
struct ngx_command_s {
    /*命令名称如http, server, listen等*/
    ngx_str_t             name ;
    /*命令类型如:NGX_HTTP_MAIN_CONF,NGX_HTTP_SRV_CONF,这里还要指定参数可以接受的参数个数或类型如NGX_CONF_TAKE1代表可接受一个参数,
     *假如我们配置:NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1表示该命令既可以出现在main_conf中也可以出现在srv_conf中,并且必须接受一个参数
     */
    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;
};

/*
 *核心模块定义数据结构
 */
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_module_s {
    /*模块在外层配置上下文中索引,假如是NGX_HTTP_MODULE,则代表自定义配置在ngx_http_ctx_t中main_conf,srv_conf,loc_conf配置的索引*/
    ngx_uint_t            ctx_index ;
    /*所有模块中此模块的索引,即全局ngx_modules数组中的位置*/
    ngx_uint_t            index ;     

    ngx_uint_t            spare0 ;
    ngx_uint_t            spare1 ;
    ngx_uint_t            spare2 ;
    ngx_uint_t            spare3 ;

    ngx_uint_t            version ;
    /*模块上下文指针,主要包括一些模块自定义的回调函数指针或数据,假如是NGX_HTTP_MODULE,则此处则是ngx_http_module_t指针*/
    void                 *ctx;
    /*模块定义可以解析的配置命令列表*/
    ngx_command_t        *commands;
    /*模块类型NGX_HTTP_MODULE,NGX_MAIL_MODULE等*/
    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模块是执行configure时生成模块列表,保存在ngx_modules.c中,以下是我本机生成的模块列表

ngx_module_t *ngx_modules [] = {
    &ngx_core_module ,
    &ngx_errlog_module ,
    &ngx_conf_module ,
    &ngx_events_module ,
    &ngx_event_core_module ,
    &ngx_epoll_module ,
    &ngx_regex_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_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_ip_hash_module ,
    &ngx_http_upstream_least_conn_module ,
    &ngx_http_upstream_keepalive_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_copy_filter_module ,
    &ngx_http_range_body_filter_module ,
    &ngx_http_not_modified_filter_module ,
    NULL
};

其中ngx_core_module,ngx_http_module,ngx_errlog_module,ngx_regex_module,ngx_events_module,ngx_mail_module属于NGX_CORE_MODULE类型,nginx启动时首先加载所有的NGX_CORE_MODULE模块,其他模块均有NGX_CORE_MODULE模块加载,例如启动时加载ngx_http_module模块,此模块会依次加载所有的NGX_HTTP_MODULE类型模块,包括加载配置和进行初始化工作。

posted @ 2015-10-09 14:42  蒲蜡  阅读(1325)  评论(0编辑  收藏  举报