看了一點nginx的源碼發現,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的模組圖,有助於同學們接下來的學習。