nginx 源码学习笔记(二)

要分析nginx源码,首先要搞懂的就是nginx的模块思想以及相关的数据结构。

还记得我们上一次写的helloworld模块么?里面涉及最重要的数据就是ngx_module_t指针数组,这个指针数组包含了当前编译版本支持的所有模块,这个指针数组定义实在自动脚本生成的objs/ngx_modules.c中,如下:

    extern ngx_module_t  ngx_core_module;  
    extern ngx_module_t  ngx_errlog_module;  
    extern ngx_module_t  ngx_conf_module;  
    extern ngx_module_t  ngx_events_module;  
    extern ngx_module_t  ngx_event_core_module;  
    extern ngx_module_t  ngx_epoll_module;  
    extern ngx_module_t  ngx_http_module;  
    extern ngx_module_t  ngx_http_core_module;  
    extern ngx_module_t  ngx_http_log_module;  
    extern ngx_module_t  ngx_http_upstream_module;  
    extern ngx_module_t  ngx_http_static_module;  
    extern ngx_module_t  ngx_http_autoindex_module;  
    extern ngx_module_t  ngx_http_index_module;  
    extern ngx_module_t  ngx_http_auth_basic_module;  
    extern ngx_module_t  ngx_http_access_module;  
    extern ngx_module_t  ngx_http_limit_zone_module;  
    extern ngx_module_t  ngx_http_limit_req_module;  
    extern ngx_module_t  ngx_http_geo_module;  
    extern ngx_module_t  ngx_http_map_module;  
    extern ngx_module_t  ngx_http_split_clients_module;  
    extern ngx_module_t  ngx_http_referer_module;  
    extern ngx_module_t  ngx_http_rewrite_module;  
    extern ngx_module_t  ngx_http_proxy_module;  
    extern ngx_module_t  ngx_http_fastcgi_module;  
    extern ngx_module_t  ngx_http_uwsgi_module;  
    extern ngx_module_t  ngx_http_scgi_module;  
    extern ngx_module_t  ngx_http_memcached_module;  
    extern ngx_module_t  ngx_http_empty_gif_module;  
    extern ngx_module_t  ngx_http_browser_module;  
    extern ngx_module_t  ngx_http_upstream_ip_hash_module;  
    extern ngx_module_t  ngx_http_cache_purge_module;  
    extern ngx_module_t  ngx_http_write_filter_module;  
    extern ngx_module_t  ngx_http_header_filter_module;  
    extern ngx_module_t  ngx_http_chunked_filter_module;  
    extern ngx_module_t  ngx_http_range_header_filter_module;  
    extern ngx_module_t  ngx_http_gzip_filter_module;  
    extern ngx_module_t  ngx_http_postpone_filter_module;  
    extern ngx_module_t  ngx_http_ssi_filter_module;  
    extern ngx_module_t  ngx_http_charset_filter_module;  
    extern ngx_module_t  ngx_http_userid_filter_module;  
    extern ngx_module_t  ngx_http_headers_filter_module;  
    extern ngx_module_t  ngx_http_copy_filter_module;  
    extern ngx_module_t  ngx_http_range_body_filter_module;  
    extern ngx_module_t  ngx_http_not_modified_filter_module;  
      
    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_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_zone_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_cache_purge_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定义在src/core/nginx.c中:

ngx_module_t  ngx_core_module = {  
    NGX_MODULE_V1,  
    &ngx_core_module_ctx,                  /* module context */  
    ngx_core_commands,                     /* module directives */  
    NGX_CORE_MODULE,                       /* module type */  
    NULL,                                  /* init master */  
    NULL,                                  /* init module */  
    NULL,                                  /* init process */  
    NULL,                                  /* init thread */  
    NULL,                                  /* exit thread */  
    NULL,                                  /* exit process */  
    NULL,                                  /* exit master */  
    NGX_MODULE_V1_PADDING  
}; 

是不是跟helloworld里面非常相似了,没错,他们都是模块,唯一的不同点就是helloworld是你另外加进去的。

到现在位置也只是初探nginx的模块,接下来的学习将会更复杂,在最后提一张别人画的nginx的模块图,有助于接下来的学习。



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值