nginx将请求的处理过程划分为11个phase(阶段),相当于是对请求处理的一种抽象,便于定制处理过程。这个11个phase,分别是(定义在http/ngx_http_core_module.h):
typedef enum {
NGX_HTTP_POST_READ_PHASE = 0, /* 读取请求 */
NGX_HTTP_SERVER_REWRITE_PHASE, /* server级别的rewrite */
NGX_HTTP_FIND_CONFIG_PHASE, /* 根据uri查找location */
NGX_HTTP_REWRITE_PHASE, /* localtion级别的rewrite */
NGX_HTTP_POST_REWRITE_PHASE, /* server、location级别的rewrite都是在这个phase进行收尾工作的 */
NGX_HTTP_PREACCESS_PHASE, /* 粗粒度的access */
NGX_HTTP_ACCESS_PHASE, /* 细粒度的access,比如权限验证、存取控制 */
NGX_HTTP_POST_ACCESS_PHASE, /* 根据上述两个phase得到access code进行操作 */
NGX_HTTP_TRY_FILES_PHASE, /* 实现try_files指令 */
NGX_HTTP_CONTENT_PHASE, /* 生成http响应 */
NGX_HTTP_LOG_PHASE /* log模块 */
} ngx_http_phases;
这些phase按照先后顺序执行,只有在rewrite之后流程会重新跳转到NGX_HTTP_FIND_CONFIG_PHASE。其中,只有7个phase可以注册handler以定制处理过程,其他的只有一个固定的handler:
NGX_HTTP_POST_READ_PHASE
NGX_HTTP_SERVER_REWRITE_PHASE,
NGX_HTTP_REWRITE_PHASE,
NGX_HTTP_PREACCESS_PHASE,
NGX_HTTP_ACCESS_PHASE,
NGX_HTTP_CONTENT_PHASE,
NGX_HTTP_LOG_PHASE
一般地,phase handler的注册都是在http模块的postconfiguration回调函数中,后面会看到为什么要在这个时间点注册。
1. 重要的数据结构
1. ngx_http_phase_t
typedef struct {
ngx_array_t handlers;
} ngx_http_phase_t;
ngx_http_core_main_conf_t的phases数组存放了所有的phase,其中每个元素是ngx_http_phase_t类型的,表示的就是对应的phase handler的数组。ngx_http_core_main_conf_t->phases数组主要用于handler的注册。
2. ngx_http_phase_engine_t
typedef struct {
/**
* 所有phase handler的数组。
*/
ngx_http_phase_handler_t *handlers;
/**
* server rewrite阶段的handler在ngx_http_core_main_conf_t->phase_engine.handlers数组中的下标
*/
ngx_uint_t server_rewrite_index;
/**
* rewrite阶段的handler在ngx_http_core_main_conf_t->phase_engine.handlers数组中的下标
*/
ngx_uint_t location_rewrite_index;
} ngx_http_phase_engine_t;
ngx_http_core_main_conf_t的phase_engine字段表示phase的执行引擎,它会把所有的phase handler组织成数组,元素是ngx_http_phase_handler_t。phase_engine会根据phases数组中注册的handler进行初始化。
3. ngx_http_phase_handler_t
struct ngx_http_phase_handler_s {
/* 执行校验,并调用handler函数,同一个phase的handler的checker相同 */
ngx_http_phase_handler_pt checker;
/* handler函数指针 */
ngx_http_handler_pt handler;
/*
* 指向下一个phase的第一个handler在ngx_http_core_main_conf_t->phase_engine.handlers数组中的下标
*
*/
ngx_uint_t next;
};
2. phase handler初始化
phase handler初始化是在http模块初始化函数ngx_http_block中完成的,摘录关键代码:
/**
* 初始化每个phase对应的handlers数组,以便在执行postconfiguration回调函数时,
* 注册phase handler。
* ngx_http_core_main_conf_t->phases数组存放所有的phase。
*/
if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
return NGX_CONF_ERROR;
}
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->postconfiguration) {
if (module->postconfiguration(cf) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
}
if (ngx_http_variables_init_vars(cf) != NGX_OK) {
return NGX_CONF_ERROR;
}
/*
* http{}'s cf->ctx was needed while the configuration merging
* and in postconfiguration process
*/
*cf = pcf;
/**
* 根据各个phase的handlers数组初始化ngx_http_core_main_conf_t->phase_engine。
* 在phase_engine中所有的handler存放在一个数组中。
*/
if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
return NGX_CONF_ERROR;
}
先调用ngx_http_init_phases为ngx_http_core_main_conf_t->phases分配空间,然后会调用所有http module的postconfiguration回调函数,最后再调用ngx_http_init_phase_handlers初始化ngx_http_core_main_conf_t->phase_engine。这就是为什么phase handler的注册只能在postconfiguration回调函数中,因为只有在它调用前phases才会分配空间。ngx_http_init_phases函数很简单,就是一堆ngx_array_t的初始化,下面看一下ngx_http_init_phase_handlers。
1. ngx_http_init_phase_handlers
static ngx_int_t
ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
{
ngx_int_t j;
ngx_uint_t i, n;
ngx_uint_t find_config_index, use_rewrite, use_access;
ngx_http_handler_pt *h;
ngx_http_