우리 모두 알고 있듯이, nginx에서 set $xxx 'hello'; 는 set 명령어를 사용하여 hello의 문자열 값을 변수 xxx로 설정하는 것입니다. 이 변수를 사용하려면 $xxx만 있으면 됩니다.
하지만 nginx에서는 다음 변수를 직접 사용하여 해당 값을 직접 얻을 수도 있습니다.
$arg_parameter name location?name=123에서 클라이언트가 요청한 매개변수 xx를 가져옵니다. 그런 다음 $arg_name은 해당 값 123입니다.
$args, 요청의 매개변수 문자열(예: name=123&age= 24)
$content_length, HTTP 요청 정보의 "Content-Length"
$content_type, 요청 정보의 "Content-Type"
$host, 요청이 없는 경우 요청 정보의 "Host" 설정된 서버 이름
$request_method, "GET", "POST" 등과 같은 요청 방법과 동일한 호스트 라인
$remote_addr, 클라이언트 주소
$remote_port, 클라이언트 포트 번호
$remote_user, 인증에 사용되는 클라이언트 사용자 이름;
$request_filename, 현재 요청된 파일 경로 이름
$request_uri, 요청된 URI, 매개변수 포함
$query_string, $args
$scheme과 동일, http 또는 https와 같이 사용된 프로토콜, 예: rewrite ^(.+)$ $scheme://example.com$1 리디렉션
$server_protocol, 요청된 프로토콜 버전, "HTTP/1.0" 또는 "HTTP/1.1 "
$server_addr, 서버 주소
$server_name, 요청에 의해 도달된 서버 이름
$server_port, 요청에 의해 도달된 서버 포트 번호
$uri, 요청된 URI는 원래 값과 다를 수 있습니다. , 예를 들어
$http_header 매개변수 이름을 사용하면 리디렉션 후 헤더 값을 얻을 수 있습니다. 예를 들어 $http_user_agent는 헤더에서 UA를 가져오는 것입니다.
...
변수 묶음 위에서 언급한 변수를 nginx 내장 변수라고 하며, 이러한 변수는 nginx가 시작될 때 로드되며, 다른 모듈은 다른 내장 변수를 로드합니다.
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或错误字符串。*/ };
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 } }
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; };
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;// }
마지막으로 변수 값을 가져오는 함수를 살펴보세요
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; }
위 내용은 nginx 모듈 개발을 소개합니다. 내용의 측면을 포함하여 nginx 내장 변수를 추가하는 것이 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.