0
假设我正在写一个声明一定的指导mymod_opt
在定位块可用的nginx的模块和用户编写类似如下:nginx模块扩展指令变量的最佳方式是什么?
location/{
mymod_opt $uri;
...
}
什么是对模块扩展的变量,使得它得到的最好方式一个ngx_str_t
与实际的URI而不是"$uri"
?
假设我正在写一个声明一定的指导mymod_opt
在定位块可用的nginx的模块和用户编写类似如下:nginx模块扩展指令变量的最佳方式是什么?
location/{
mymod_opt $uri;
...
}
什么是对模块扩展的变量,使得它得到的最好方式一个ngx_str_t
与实际的URI而不是"$uri"
?
看起来最好的方法是将变量作为脚本处理,当编译第一次处理配置时使用函数ngx_http_script_compile
,当调用模块来扩展变量时使用ngx_http_script_run
。以下是如何设置它的一个示例:
在模块的本地配置中添加变量以存储发出的脚本。
struct ngx_http_mymod_loc_conf_t {
ngx_array_t* opt_lengths;
ngx_array_t* opt_values;
};
在命令数组中指定在遇到指令时运行自定义函数。
static ngx_command_t ngx_http_mymod_commands[] = {
{
ngx_string("mymod_opt"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_http_mymod_opt,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
...
}
添加功能,编译脚本
static char *ngx_http_mymod_opt(ngx_conf_t *cf, ngx_command_t *command,
void *conf) {
ngx_http_mymod_loc_conf_t *loc_conf;
ngx_str_t *value;
ngx_str_t *mymod_opt;
ngx_http_script_compile_t script_compile;
loc_conf = conf;
value = cf->args->elts;
mymod_opt = &value[1];
ngx_memzero(&script_compile, sizeof(ngx_http_script_compile_t));
script_compile.cf = cf;
script_compile.source = mymod_opt;
script_compile.lengths = &loc_conf->mymod_opt_lengths;
script_compile.values = &loc_conf->mymod_opt_values;
script_compile.variables = ngx_http_script_variables_count(mymod_opt);
script_compile.complete_lengths = 1;
script_compile.complete_values = 1;
if (ngx_http_script_compile(&script_compile) != NGX_OK) return NGX_CONF_ERROR;
return NGX_CONF_OK;
}
然后变量可以用这个函数调用后扩大:
ngx_str_t result;
ngx_http_script_run(request, &result, loc_conf->lengths->elts, 0,
loc_conf->values->elts)