nginx源码分析之script (一)

本文主要探讨nginx源码中的变量 ngx_http_variable_t,它在ngx_http_core_main_conf_t结构中的应用,以及如何利用这些变量进行重定向操作。通过代码分析,揭示了nginx如何处理字符串、数组和字符等数据类型。
摘要由CSDN通过智能技术生成
nginx的变量的处理流程可以分为 变量的创建与变量对应值的获取流程。
nginx的变量存储也可以分为两部分 ngx_http_core_main_conf_t用于存放变量创建时的相关成员,ngx_http_request_t保存的变量对应的值。


1.nginx变量 相关的结构体:

a.用于代表变量的ngx_http_variable_t,该变量被适用于ngx_http_core_main_conf_t

[code=C/C++]

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; //用于获取该变量对应的值,用得比较多,两个handler中设置一个即可。
     //typedef ngx_int_t (*ngx_http_get_variable_pt) (ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
     uintptr_t data; //传入get_handler或者set_handler的第三个参数data.
     ngx_uint_t flags;
     ngx_uint_t index; //当我们建立索引变量时, 会在
};
[/code]

b.代表某个变量对应value的ngx_http_variable_value_t,
该成员一般用于ngx_http_request_s的ngx_http_variable_value_t *variables成员.
ngx_http_request_s的variables成员是用于缓冲所有indexed变量的值,初始化该数组的操作在ngx_http_init_request;
//r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts * sizeof(ngx_http_variable_value_t));

typedef struct {
unsigned len:28;//变量值的长度

unsigned valid:1; //用于标识变量是否有效。
unsigned no_cacheable:1;
unsigned not_found:1;
unsigned escape:1;

u_char *data; //变量对应的值。
} ngx_variable_value_t;



2. 变量的创建流程,以nginx map模块的变量创建过程为例,

首先对于ngx_http_core_variable变量进行简单的演示。

因为nginx的core_variable 是core模块分配的static ngx_http_variables_t数组
即ngx_http_core_variables[];从而大量的创建、注册操作就节省了。
static ngx_http_variable_t ngx_http_core_variables[] = {
{ ngx_string("http_host"), NULL, ngx_http_variable_header,//可知core变量在定义时已经设置了get_handler.
offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
.....
};

其次开始介绍ngx_http_map_block中对于变量的解析.
map配置格式:
map $uri $new {
default http://www.domain.com/home/; //默认值

/aa http://aa.domain.com/; //当$uri匹配/aa时 $new对应的值为http://aa.domain.com/。
/bb http://bb.domain.com/;
/john http://my.domain.com/users/john/;
}

相应的解析函数为ngx_http_map_block()
static char *
ngx_http_map_block(ng
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值