Nginx

Nginx:指令工作原理浅析

模块和指令如何集成到 NGX 对于一个自定义模块,要了解工作原理,则先了解组成。只需要简单过一遍这几个结构: ngx_command_t ngx_http_module_t ngx_module_s ngx_command_t 首先,模块有自己的配置信息。命名方式为 ngx_http_<module name>_(main|srv|loc)_conf_t 然后,会通过一个静态数组定义指令。(ngx_null_command 标记结尾) 1static ngx_command_t ngx_http_hello_commands[] = { 2 { 3 // ngx_str_t name; 命令名称 4 ngx_string("hello_string"), 5 // ngx_uint_t type; 命令类型。这里表示是一个能出现在 location 块的配置项,且支持 0 或 1 个参数 6 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1, 7 // char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 这里是指定命令的处理函数指针。当解析到这个配置项时,会调用这个函数 8 ngx_http_hello_string, 9 // ngx_uint_t conf; 当前配置项存储的内存位置。有三个内存池:main,srv,loc。这里表示存储在 loc 内存池中 10 NGX_HTTP_LOC_CONF_OFFSET, 11 // ngx_uint_t offset; 指定该配置项值的精确存放位置。这里表示存储在 ngx_http_hello_loc_conf_t 结构体中的 hello_string 成员中 12 offsetof(ngx_http_hello_loc_conf_t, hello_string), 13 // void *post; 不知道,不用管,用到再说。 14 NULL }, 15 16 ngx_null_command 17}; ngx_http_module_t 每个模块都有一个 ngx_http_module_t 类型的结构体变量,用于提供一组回调指针。这样当遇到一些情况,就能通知到我们自己的模块。包括这些: Read more...
1 of 1