nginx模組開發-增加nginx內建變數

來源:互聯網
上載者:User
眾所周知,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或錯誤字串。*/};
定義模組的ctx的時候在postconfiguration階段定義載入內建變數的函數。
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 }}  
ngx_http_variable_t 的定義在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;};
flag屬性就是由下面的幾個屬性群組合而成:
#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表示這個變數每次都要去取值,而不是直接返回上次cache的值(配合對應的介面).
NGX_HTTP_VAR_INDEXED表示這個變數是用索引讀取的.
NGX_HTTP_VAR_NOHASH表示這個變數不需要被hash.
static ngx_int_tngx_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;//}
在postconfiguration的回調中將自訂的變數加入nginx中,這樣就可以直接在設定檔中用$x_request_uri獲得ngx_http_x_request_uri_variable函數的返回值。

最後看一下擷取變數值的函數

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教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.