http配置项解析编程
配置config
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
在nginx_http_mytest_module.c中定义mytest模块,使用预设方法解析test_flag,test_str,test_num配置项,使用自定义配置项处理方法解析mytest项。
<pre>#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
//create a structure
typedef struct{
ngx_str_t my_str;
ngx_int_t my_num;
ngx_flag_t my_flag;
ngx_str_t mytest_str;
ngx_int_t mytest_num;
} ngx_http_mytest_conf_t;
static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r);
static void* ngx_http_mytest_create_loc_conf(ngx_conf_t* cf);
static char* ngx_http_mytest_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
//set the resolution of configuration items
static ngx_command_t ngx_http_mytest_commands[] = {
{
//test_flag configuration item
ngx_string("test_flag"),
NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_flag),
NULL
},
{
//test_str configuration item
ngx_string("test_str"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_str),
NULL
},
{
//test_num configuration item
ngx_string("test_num"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_num),
NULL
},
{
//mytest configuration item
ngx_string("mytest"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
ngx_conf_set_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_mytest_create_loc_conf, //create location configuration
ngx_http_mytest_merge_loc_conf //merge the loc configurations
};
ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
{
ngx_http_mytest_conf_t* mycf = conf;
ngx_str_t* value = cf->args->elts;
mycf->mytest_num = ngx_atoi(value[2].data, value[2].len);