您的位置:首页 > 运维架构 > Nginx

Nginx内部变量使用

2015-09-01 13:31 686 查看
Nginx的内部变量指nginx官方模块中所导出的变量,大部分常用的变量都是CORE HTTP模块导出。在nginx中,可以在模块开发代码 中使用变量,也可以在配置文件中使用。

配置文件中使用方法:

在配置文件中使用内部变量,只需要在变量名前加一个”$”符号就可以了,eg,使用http模块的host变量,写成($host)形式。

模块代码中使用方法:

在模块代码中使用内部变量较为麻烦,nginx提供了以下一些接口来取得变量:

ngx_http_variable_value_t *ngx_http_get_indexed_variable(ngx_http_request_t *r,
ngx_uint_t index);
ngx_http_variable_value_t *ngx_http_get_flushed_variable(ngx_http_request_t *r,
ngx_uint_t index);
ngx_http_variable_value_t *ngx_http_get_variable(ngx_http_request_t *r,
ngx_str_t *name, ngx_uint_t key);


其中,前两个函数都是用来取得有索引的变量,区别在于后一个会处理NGX_HTTP_VAR_NOCACHEABLE这个标记。而最后一个函数可以取得没有索引的变量值。

Nginx也提供了获得变量索引的接口:

ngx_int_t ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name);


模块开发中使用的例子:

static ngx_int_t index_1;
static char * ngx_http_op(ngx_conf_t *cf,ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
clcf->handler = ngx_http_op_handler;
//定义变量名
ngx_str_t name = ngx_string("name of variable");
//得到该变量名的索引值
index_1 = ngx_http_get_variable_index(cf,&name);
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_op_handler(ngx_http_request_t *r)
{
u_char ngx_string[1024]={0};    //存储返回的字符串
ngx_http_variable_value_t *value;

value = ngx_http_get_indexed_variable(r,index);
if (value == NULL || value->not_found){
ngx_sprintf(ngx_string,"value is not exsit!");
}
else {
ngx_sprintf(ngx_string,"value:%s",value->data);
}
}


自定义变量创建

在Nginx中,我们也可以自定义创建变量。创建变量有两种方式,分别是在配置文件中使用set指令,以及在模块中调用对应的接口。

配置文件中创建:

Nginx的ngx_http_rewrite_module模块提供了set配置指令



我们可以在配置文件中直接使用它:

set $a "hello"; #对变量a进行赋值操作,把字符串hello赋值给a


模块调用中创建:

Nginx中提供下面的接口创建变量:

/*
** 该函数将变量"name"添加进全局hash key表
*/
ngx_http_variable_t *ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags);


而变量结构体(ngx_http_variable_s)是以下结构:

struct ngx_http_variable_s {
ngx_str_t                     name;   /* must be first to build the hash */
ngx_http_set_variable_pt      set_handler;
ngx_http_get_variable_pt      get_handler;
uintptr_t                     data;
ngx_ui
4000
nt_t                    flags;
ngx_uint_t                    index;
};


其中,name是变量名字,set/get_handler表示对应的设置以及读取回调,data是传递给回调的参数,flags表示变量属性,index是该变量的索引。

其中,flag属性是以下几个属性的组合

#define NGX_HTTP_VAR_CHANGEABLE   1     /* 变量可变 */
#define NGX_HTTP_VAR_NOCACHEABLE  2     /* 不直接返回上次cache的值,每次都需要去取值 */
#define NGX_HTTP_VAR_INDEXED      4     /* 变量使用索引读取 */
#define NGX_HTTP_VAR_NOHASH       8     /* 变量不需要被hash */


变量在nginx中初始化时,首先在解析HTTP之前会调用ngx_http_variables_add_core_vars(pre_config)来将HTTP core模块导出的变量(http_host/remote_addr…)添加进全局的hash key链中,然后调用ngx_http_variables_init_vars来初始化所有的变量(包括其他HTTP模块的变量,以及在配置文件中通过set指令设置的变量),当请求过来时会给每个请求创建一个变量数组,只有取变量值的时候,才会将变量保存在对应的变量数组位置。

模块开发中创建变量的例子(ngx_http_ssl_module.c):



ssl模块中,变量存放在一个ngx_http_variable_t数组中:

static ngx_http_variable_t  ngx_http_ssl_vars[] = {

{ ngx_string("ssl_protocol"), NULL, ngx_http_ssl_static_variable,
(uintptr_t) ngx_ssl_get_protocol, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_cipher"), NULL, ngx_http_ssl_static_variable,
(uintptr_t) ngx_ssl_get_cipher_name, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_session_id"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_session_id, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_session_reused"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_session_reused, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_server_name"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_server_name, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_cert"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_certificate, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_raw_cert"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_raw_certificate,
NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_s_dn"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_subject_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_i_dn"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_issuer_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_serial"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_serial_number, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_fingerprint"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_fingerprint, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_client_verify"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_client_verify, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: