24,854
社区成员
发帖
与我相关
我的任务
分享
//先定义变量
static ngx_int_t keywordIndex;
static ngx_str_t keywordFlg = ngx_string("keyword");
ngx_http_variable_value_t *keyword_vv;
//定义增加变量函数
static ngx_int_t
ngx_http_suggest_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t*v, uintptr_t data)
{
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_suggest_add_variable(ngx_conf_t *cf, ngx_str_t *name)
{
ngx_http_variable_t *v;
v = ngx_http_add_variable(cf, name, NGX_HTTP_VAR_CHANGEABLE);
if(v == NULL)
{
return NGX_ERROR;
}
v->get_handler = ngx_http_suggest_variable_not_found;
return ngx_http_get_variable_index(cf, name);
}
//添加变量
ngx_int_t
ngx_http_module_init(ngx_conf_t *cf)
{
printf("called:ngx_http_module_init==>\n");
if((keywordIndex = ngx_http_suggest_add_variable(cf, &keywordFlg)) == NGX_ERROR)
{
return NGX_ERROR;
}
。。。
}
//handler中取用变量
static ngx_int_t
ngx_suggest_handler(ngx_http_request_t *r)
{
printf("called:ngx_suggest_handler\n");
keyword_vv = ngx_http_get_indexed_variable(r, keywordIndex);
// 注意:keyword_vv
// 这个ngx_http_variable_value_t结构中有两个变量,data参数值起始位置,len,值长度。
// OK了,再用这两个参数去取值。
/*
27 typedef struct {
28 unsigned len:28;
29
30 unsigned valid:1;
31 unsigned no_cacheable:1;
32 unsigned not_found:1;
33 unsigned escape:1;
34
35 u_char *data;
36 } ngx_variable_value_t;
*/
。。。
}