nginx变量机制

本文介绍了Nginx配置文件中以$开头的变量,包括nginx自定义和用户自定义变量。在配置解析前,nginx将自定义变量规划到http核心配置的variables_keys字段。各模块通过预配置过程添加自己的变量。对于用户定义变量,它们会被添加到variables中。文章还详细探讨了变量的属性如changeable和nocacheable,以及data、get_handler和set_handler的含义,并展示了用户自定义变量如何设置index。
摘要由CSDN通过智能技术生成

在设置nginx配置文件的时候,配置文件中会出现$file等以$开头的字符串。这些以$开头的字符串在nginx中就是所谓的变量。nginx的变量分为nginx自定义的变量以及用户自定义的变量。nginx自定义的变量有很多,每个模块也可以定义自己的变量,例如ngx_http_core_variables这个就包含了很多nginx自定义的变量:

static ngx_http_variable_t  ngx_http_core_variables[] = {

    { ngx_string("http_host"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.host), 0, 0 },

    { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },

    { ngx_string("http_referer"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },

#if (NGX_HTTP_GZIP)
    { ngx_string("http_via"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
#endif

#if (NGX_HTTP_X_FORWARDED_FOR)
    { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
#endif

    { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
      offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },

    { ngx_string("content_length"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },

    { ngx_string("content_type"), NULL, ngx_http_variable_header,
      offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
.....
}
而用户自定义的变量一般都是用set命令来定义的。下面先来介绍两个关于变量的结构体:

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_uint_t                    flags;
    ngx_uint_t                    index;
};
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;
从结构体名字来看这两个结构体差不多能构成key-value。但是在ngx_variable_value_t本身也带有data,这个data看似是存放值的地方,那为什么还需要用到ngx_variable_value_t呢?等到下面再讲。

在解析配置文件之前,nginx会把所有nginx自定义的变量统一规划起来,规划到http核心配置ngx_http_core_main_conf_t的variables_keys字段内。nginx几乎每个模块都可以定义自己的变量,每个模块都是通过自己的preconfiguration把变量统一规划起来的。

static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
......
 for (m = 0; ngx_modules[m]; m++) {
        if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
            continue;
        }

        module = ngx_modules[m]->ctx;

        if (module->preconfiguration) {
            if (module->preconfiguration(cf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
        }
    }

    /* parse inside the http{} block */

    cf->module_type = NGX_HTTP_MODULE;
    cf->cmd_type = NGX_HTTP_MAIN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值