解剖Nginx·模組開發篇(4)模組開發中的命名規則和模組載入與執行流程

鍾超發表於2012-06-03

解剖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包含serverlocation等模組,所以在解析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

-

相關文章