眾所周知,nginx中set $xxx 'hello';就是用set指令給變數xxx設定hello的字串值,在nginx中呼叫值的話,只需要$xxx就可以使用這個變數。
然而在nginx中,我們也可以直接使用下面的變量,直接就可以取得對應的值。
$arg_參數名稱 在location中取得客戶端請求的參數xx?name=123 那$arg_name就是對應的值123
$args, 請求中的參數字串例如name=123&age=24
$content_length, HTTP請求資訊裡的"Content-Length"
$content_type, 請求資訊裡的"Content-Type"
$host, 請求資訊中的"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 redirect
$server_protocol, 請求的協定版本,"HTTP/1.0"或"HTTP/1.1"
$server_addr, 伺服器位址
$server_name, 請求到達的伺服器名稱
$server_port, 要求到達的伺服器連接埠號碼
$uri,請求的URI,可能和最初的值有不同,例如經過重定向之類的
$http_header參數名 可以用來獲得header的值,例如$http_user_agent 就是取得header中的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教程有興趣的朋友有所幫助。