在編寫HTTP第三方模塊時,需要了解nginx中HTTP模塊的數據結構
定義HTTP模塊方式很簡單
ngx_module_t ngx_http_mytest_module;
就是這麼簡單,
下面先分析一下nginx模塊中的所有成員:
typedef struct ngx_module_s ngx_module_t; struct ngx_module_s { //分別賦值ctx_index.index.spare0.spare2.sapre3.version #define NGX_MODULE_V1 0,0,0,0,0,0,1 //同一類模塊的序號,有管理這類模塊的nginx核心模塊設置。很重要。因爲nginx模塊依賴於順序。 ngx_uint_t ctx_index; /*nginx啓動時根據ngx_modules數組設置各模塊的index值 ngx_max_module = 0; for(i = 0; ngx_modules[i];i++) { ngx_modules[i]->index = ngx_max_module++; } */ //index表示當前模塊在ngx_modules中的序號 ngx_uint_t index; //spare系列保留變量,暫時沒有使用 ngx_uint_t spare0; ngx_uint_t spare1; ngx_uint_t spare2; ngx_uint_t spare3; //模塊的版本號 ngx_unit_t version; //用於指向一類模塊的上下文結構體。ctx指向特定類型模塊的公共接口 //例:http模塊中,ctx指向ngx_http_module_t結構體 void *ctx; //nginx.conf配置項 ngx_command_t *command; //表示模塊類型。與ctx相關。 //5種取值。NGX_HTTP_MODULE.NGX_CORE_MODULE,NGX_CONF_MODULE.NGX_EVENT_MODULE //NGX_MAIL_MODULE ngx_uint_t type; //下面是7個函數指針表示有7個執行點會分別調用這7種方法。 //1.啓動時回調init_master ngx_int_t (*init_master)(ngx_log_t *log); //2.init_module初始化所有模塊時被調用。master/worker模式,在啓動worker子進程前完成 ngx_int_t (init_module)(ngx_cycle_t *cycle); //3.正常服務前被調用,master/worker模式,多個worker已經殘生,每個worker初始化會調用 ngx_int_t (init_process)(ngx_cycle_t *cycle); //4.這個可以忽視,就是代表多線程模式的初始化,Nginx不支持多線程。1.015版本是這樣,後面不清楚 ngx_int_t (init_thread)(ngx_cycle_t *cycle); //5.同上 void (*exit_thread)(ngx_cycle_t *cycle); //6.服務停止前調用,worker進程在退出前調用 void (*exit_process)(ngx_cycle_t *cycle); //7.master進程退出前調用 void (*exit_master)(ngx_cycle_t *cycle); //保留字段,可以使用NGX_MODULE_V1_PADDING填充,同第一個 uintprt_t spare_hook0; uintprt_t spare_hook1; uintprt_t spare_hook2; uintprt_t spare_hook3; uintprt_t spare_hook4; uintprt_t spare_hook5; uintprt_t spare_hook6; uintprt_t spare_hook7; };
ps:定義一個HTTP模塊時,type字段必須設置爲NGX_HTTP_MODULE;
ngx_module_t 中的ctx必須指向ngx_http_module_t接口。
-------------------------------------------------------------------------------------------
下面來解釋一下ngx_http_module_t結構,他描述了HTTP礦在在讀取。重載配置是定義的8個階段。啓動過程中調用。NULL則不調用
typedef struct { //解析配置文件前調用 ngx_int_t (*preconfiguration)(ngx_conf_t *cf); //完成配置文件的解析後調用 ngx_int_t (*postconfiguration)(ngx_conf_t *cf); //創建數據結構用於存儲只屬於http{...}塊的全局配置項。 void* (*create_main_conf)(ngx_conf_t *cf); //初始化上面這個 char* (*init_main_conf)(ngx_conf_t *cd,void *conf); //srv級別(server{...}快)配置項 void* (*create_srv_conf)(ngx_conf_t *cf); //合併main/srv級別下的同名配置項 char* (*merge_srv_conf)(ngx_conf_t *cd,void *prev,void* conf); //loc級別(location{...}快)配置項 void* (*create_loc_conf)(ngx_conf_t *cf); //合併loc/srv級別下的同名配置項 char* (*merge_srv_conf)(ngx_conf_t *cd,void *prev,void* conf); }
ps:回調方法與內部無關,與nginx.conf配置項有關。
-------------------------------------------------------------------------------------------
這裏在提一個概念,commadns數組(元素爲ngx_command_t)。用於定義模塊的配置文件。ngx解析配置文件中的一個配置項首先會遍歷所有模塊,對於一個模塊而言,通過遍歷commands數組進行,ngx_null_command爲結束。
typedef struct ngx_command_s ngx_commadn_t; struct ngx_command_s { //配置項名稱,如“gzip” ngx_str_t name; //配置項類型,制定出現位置,以及可以攜帶的參數個數 ngx_uint_t type; //出現了Name中指定的配置項後,將會調用set處理配置項的參數 char* (*set)(ngx_conf_t *cf,ngx_command_t *cmd,void* conf); //配置文件中的偏移量 ngx_uint_t conf; //使用預設的解析方法解析配置項 ngx_uint_t offset; //配置項讀取後的處理方法,必須是ngx_conf_post_t結構的指針 void *post; } ngx_null_command爲一個空的ngx_command_t. #define ngx_null_command {ngx_null_string,0,NULL,0,0,NULL}
以上