解剖Nginx·模塊開發篇(4)模塊開發中的命名規則和模塊加載與運行流程
- 作者:柳大·Poechant(鍾超)
- 郵箱:zhongchao.ustc#gmail.com(# -> @)
- 博客:Blog.CSDN.net/Poechant
- 日期:June 2nd, 2012
1 命名規則
1.1 基本變量
基本變量有三個:
- ngx_module_t 類型的 ngx_http_foo_bar_module;
- ngx_command_t 類型的數組 ngx_http_foo_bar_commands;
- ngx_http_module_t 類型的 ngx_http_foo_bar_module_ctx。
假設你開發了一個 Foo Bar 模塊,那麼模塊名稱應該叫:
ngx_http_foo_bar_module
命令集合的名字的命名規則:
ngx_http_foo_bar_commands
上下文的明子的命名規則:
ngx_http_foo_bar_module_ctx
1.2 基本類型
模塊配置
ngx_http_foo_bar_<main|srv|loc>_conf_t
2 加載與運行流程
這與 ngx_http_foo_bar_module_ctx 很有關係,它是 ngx_http_module_t 類型的,該類型定義如下:
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;
2.1 preconfiguration
調用ngx_http_foo_bar_module_ctx.preconfiguration
初始化
http 組件和 nginx 其他組件的交互;
2.2 解析配置文件
解析配置文件中的http
模塊。http
包含server
、location
等模塊,所以在解析http
組件時,會根據具體的配置情況,多次調用ngx_http_foo_bar_module_ctx.create_(srv|loc)_conf
,創建
main_conf、srv_conf、loc_conf;
2.3 初始化 http 組件的 main 部分
調用ngx_http_foo_bar_module_ctx.init_main_conf
初始化
main 組件;
2.4 merge
調用ngx_http_foo_bar_module_ctx.merge_srv_conf
合併那些定義在“http”組件中的“server”組件配置。調用ngx_http_foo_bar_module_ctx.merge_loc_conf
合併那些定義在上層組件中的“location”配置;
2.5 postconfiguration
調用ngx_http_foo_bar_module_ctx.postconfigation
初始化
http 組件和 nginx 其他組件的交互。
-
轉載請註明來自柳大的CSDN博客:Blog.CSDN.net/Poechant