Home > Backend Development > PHP Tutorial > nginx module development-adding nginx built-in variables

nginx module development-adding nginx built-in variables

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:06:40
Original
1352 people have browsed it

As we all know, set $xxx 'hello'; in nginx is to set the string value of hello to the variable xxx using the set instruction. If you call the value in nginx, you only need $xxx to use this variable.

However, in nginx, we can also directly use the following variables to get the corresponding values ​​directly.

$arg_parameter name Get the parameter xx requested by the client in location?name=123 Then $arg_name is the corresponding value 123
$args, the parameter string in the request such as name=123&age=24
$content_length, HTTP "Content-Length"
$content_type in the request information, "Content-Type"
$host in the request information, "Host" in the request information. If there is no Host line in the request, it is equal to the set server name
$request_method , request method, such as "GET", "POST", etc.
$remote_addr, client address
$remote_port, client port number
$remote_user, client user name, for authentication;
$request_filename, current requested file path Name
$request_uri, the URI of the request, with parameters
$query_string, the same as $args
$scheme, the protocol used, such as http or https, such as rewrite ^(.+)$ $scheme://example.com$1 redirect
$server_protocol, the protocol version of the request, "HTTP/1.0" or "HTTP/1.1"
$server_addr, the server address
$server_name, the name of the server where the request arrives
$server_port, the port number of the server where the request arrives
$uri, The requested URI may be different from the original value. For example, after redirection, etc.
$http_header parameter name can be used to obtain the value of the header. For example, $http_user_agent is to obtain the UA in the header.
...
A bunch of the above Variables are called nginx's built-in variables. These variables are loaded when nginx starts, and different modules will load different built-in variables.

static ngx_http_module_t ngx_http_x_module_ctx = {
    NULL, /* preconfiguration 在创建和读取该模块的配置信息之前被调用。*/
    ngx_http_x_post_config,      /* postconfiguration 在创建和读取该模块的配置信息之后被调用。*/

    NULL, /* create main configuration 调用该函数创建本模块位于http block的配置信息存储结构。该函数成功的时候,返回创建的配置对象。失败的话,返回NULL。*/
    NULL, /* init main configuration 调用该函数初始化本模块位于http block的配置信息存储结构。该函数成功的时候,返回NGX_CONF_OK。失败的话,返回NGX_CONF_ERROR或错误字符串*/

    NULL, /* create server configuration 调用该函数创建本模块位于http server block的配置信息存储结构,每个server block会创建一个。该函数成功的时候,返回创建的配置对象。失败的话,返回NULL。*/
    NULL, /* merge server configuration 因为有些配置指令既可以出现在http block,也可以出现在http server block中。那么遇到这种情况,每个server都会有自己存储结构来存储该server的配置,
    									但是在这种情况下http block中的配置与server block中的配置信息发生冲突的时候,就需要调用此函数进行合并,该函数并非必须提供,当预计到绝对不会发生需要合并的情况的时候,就无需提供。当然为了安全起见还是建议提供。
    								该函数执行成功的时候,返回NGX_CONF_OK。失败的话,返回NGX_CONF_ERROR或错误字符串。*/

    NULL,  	/* create location configuration 调用该函数创建本模块位于location block的配置信息存储结构。每个在配置中指明的location创建一个。该函数执行成功,返回创建的配置对象。失败的话,返回NULL。*/
    NULL    /* merge location configuration 与merge_srv_conf类似,这个也是进行配置值合并的地方。该函数成功的时候,返回NGX_CONF_OK。失败的话,返回NGX_CONF_ERROR或错误字符串。*/
};
Copy after login
defines the function to load built-in variables in the postconfiguration phase when defining the ctx of the module. The definition of
static ngx_http_variable_t ngx_http_x_variables[] = {// 定义echo模块的变量
    { ngx_string("x_request_method"), NULL,
      ngx_http_x_request_method_variable, 0,
      NGX_HTTP_VAR_NOCACHEABLE, 0 },

    { ngx_string("x_request_uri"), NULL,
      ngx_http_x_request_uri_variable, 0,
      0, 0 },
	  ...
    { ngx_null_string, NULL, NULL, 0, 0, 0 }
}  
Copy after login
ngx_http_variable_t is in ngx_http_variables.h

typedef struct ngx_http_variable_s ngx_http_variable_t;

struct ngx_http_variable_s {
    ngx_str_t                     name;   //变量的名称
    ngx_http_set_variable_pt      set_handler;//变量的设置函数回调
    ngx_http_get_variable_pt      get_handler;//变量的获取函数回调
    uintptr_t                     data;//传递给回调的参数
    ngx_uint_t                    flags;//flags表示变量的属性,index提供了一个索引(数组的脚标)
    ngx_uint_t                    index;
};
Copy after login
flag attribute is composed of the following attributes:
#define NGX_HTTP_VAR_CHANGEABLE 1
#define NGX_HTTP_VAR_NOCACHEABLE 2
#define NGX_HTTP_VAR_INDEXED 4
#define NGX_HTTP_VAR_NOHASH 8
NGX_HTTP_VAR_CHANGEABLE
NGX_HTTP_VAR_NOCACHEABLE indicates that this variable is used every time All have to get the value, instead of directly returning the value of the last cache (cooperating with the corresponding interface).
NGX_HTTP_VAR_INDEXED means that this variable is read using an index.
NGX_HTTP_VAR_NOHASH means that this variable does not need to be hashed.
static ngx_int_t
ngx_http_x_post_config(ngx_conf_t *cf)
{
       ...
	增加自己的var 变量到nginx里面
	ngx_http_variable_t *var, *v;

    for (v = ngx_http_x_variables; v->name.len; v++) {
        var = ngx_http_add_variable(cf, &v->name, v->flags);
        if (var == NULL) {
            return NGX_ERROR;
        }
        var->get_handler = v->get_handler;
        var->data = v->data;
    }
    return NGX_OK;//
}
Copy after login
adds custom variables to nginx in the postconfiguration callback, so that you can directly use $x_request_uri to obtain the return value of the ngx_http_x_request_uri_variable function in the configuration file.

Finally take a look at the function to get the variable value

ngx_int_t ngx_http_x_request_uri_variable(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    if (r->uri.len) {
        v->len = r->uri.len;
        v->valid = 1;
        v->no_cacheable = 1;
        v->not_found = 0;
        v->data = r->uri.data;

    } else {
        v->not_found = 1;
    }

    return NGX_OK;
}
Copy after login


The above introduces nginx module development - adding nginx built-in variables, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template