首頁  >  文章  >  後端開發  >  nginx HTTP模組組成

nginx HTTP模組組成

WBOY
WBOY原創
2016-08-08 09:23:09811瀏覽

原文連結:http://cjhust.blog.163.com/blog/static/17582715720124544047608/

115 ngx_conf_s

struct ngx_conf_s {

    char            ngx_array_t          *args;             //

 

    ngx_cycle_t         參數    ngx_pool_t           

ngx_pool_t           *temp_pool;

    文件資訊 「./conf/main.conf」

              / /日誌

 *  ctx;                    //(void ****)cycle->conf_ctx,而裝的是所有模組的設定資訊

    ngx_uint_t            的模組的型別    ngx_uint_t          指令的類型

     ng

    char               上面的handler使用};

備};備};

ngx_conf_s

起著橋樑的作用。 ngx_http_conf_ctx_t

typedefstruction

    void        **main_conf;

       void        **loc_conf;

} ngx_http_conf_ctx_t; HTTP 區塊

中的設定結構主要分為3

中,main ngx_http_module_t (*預先設定)(ngx_conf_ t *cf);    ngx_int_t   (*設定後)(ngx_conf_t * cf); 

  

    char       *( *init_main_conf)(ngx_conf_t *cf, void * conf);

 

    void     

    char       *(*merge_srv _conf)(ngx_conf_t *cf, void *prev, void *conf);

 

    void         char       *(*merge_loc_conf) )(ngx_conf_t *cf, void *prev, void *conf);

} ngx_http_module_t;

備註: 主要有上面

8個函數組成。

ngx_command_s

struct x_command_s

struct x_command_s      名詞;

    ngx_uint_t              *(*set)(ngx_conf_t *cf, ngx_command_t * cmd, void *conf);

    ngx_uint_t            偏移;

    void                 *post;

}; x_null_command  { ngx_null_string, 0, NULL, 0, 0, 空}

2HTTP ctx

2.1 create_main_conf(ngx_conf_t *cf)

,cf. 回傳值: void *

備註:_Yommo_cf

gx_http _get_module_ main _conf的結果;範例:oid_cf. {

  ngx_http_barrier_conf_t  *conf;   / /自訂的一個結構

  conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_barrier_conf_t)); {

    return NULL;

  } 

  conf->enable = NGX_CONF_UNSET;

  return conf;                                         //

返回自定義的結構

}

2.2 create_srv_conf(ngx_conf_t *cf)

_參數

回傳值:

void *

備註:回傳值可以作為ngx_http_conf_get_get_y_hkq; _get_module_ srv_conf的結果;

範例:oid_cf. {

  ngx_http_barrier_conf_t  *conf;   // 自訂的一個結構  conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_barrier_conf_t)); return NULL;  }

   conf->enable = NGX_CONF_UNupturn;                                 ///已回復

}

2.3 create_loc_conf(ngx_conf_t *cf)

參數:

回傳值:

void *

備註:

回傳值可以作為

ngx_http_conf_get_smm_Lot; _get_module_loc_conf

的結果;

範例:

_cf;

{

  ngx_http_barrier_conf_t  *conf;   // 自訂的一個結構

  conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_barrier_conf_t));  //init by pcalloc

  }

  conf->enable = NGX_CONF_UNSET            //

回傳自訂的結構

}_cf 參數:

ngx_conf_t *cf

,配置結構體;

備註:回傳值只是作為操作是否正確的判斷;

範例:_pstatic ngx_conf_t *cf){

    ngx_http_variable_t  *var, *v; 

          var = ngx_http_add_variable(cf, &v->name, v- >flags);

        if (var == NULL) {

        }

 

        var->get_handler = v->get_handler;

var->數據 = v->data;

    }

 

  2.5 init_main_conf(ngx_conf_t *cf, void *conf)

參數:

ngx_conf_t main

時的回傳值,在這裡作為參數;

回傳值:

char *

NGX_CONFOK

NGX_CONFOK

備註:

回傳值只是作為操作是否正確的一個判斷;

範例:static char *🎎 {    ngx_http_core_main_conf_t *cmcf = conf;//create_main時是未配置if (cmcf->L_names_hash__hash_        cmcf->server_names_hash_max_size = 512;    //這裡是

init    }  UINT) {        cmcf->server_names_hash_bucket_size = ngx_cacheline_size;

 

    cmcf->server_names_hash_bucket_size =

            ngx_align(cmcf->server_names_hash_bucket_size, ngx_cacheline_size);

    if (cmcf->variables_h ash_max _size == NGX_CONF_UNSET_UINT) {

       

    }

 

    if (cmcf->variables_hash_bucket_size ====   cmcf->data_hash_bucket_size = 64;

    }

 

_cmb; =

               ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);  if (cmcf ->ncaptures) {

        cmcf->n捕獲= (cmcf->n捕獲+ 1) * 3;

    }

 

  6 merge_srv_conf( ngx_conf_t *cf,無效*prev,無效 *conf)

參數:

ngx_conf_t配置結構體,

conf

伺服器

的配置結構體;

prev=cf->ctx.srv_conf[x_index],c/pan>

,正確的回傳值是

NGX_CONF_OK

備註:備註: static char * ngx_http_barrier_merge_conf(ngx_conf_t *cf, void *parent, void *child){ x_http_barrier_conf _ t *conf = 孩子;   if (conf->shm_zone == NULL){     *conf = *pre   ngx_conf_merge_value( conf-& > 促成、預防-> 促成, 0);  //default is 0

  return NGX_CONF_OK;} 啟用=1

,而

伺服器{}啟用= NGX_CONF_UNSET,則將

伺服器

enable=main

enable=1 loc_conf (ngx_conf_t *cf, void *prev, void *conf)

原理同

merge_srv_conf(ngx_conf_t *cf, void *prev, void *conf)

2.8 postconfiguration(ngx_conf_t *cf)

參數:

回傳值:

ngx_int_t

,正確的回傳值是

NGX_OK

備註:

備: 範例:

static ngx_int_t ngx_http_tracker_init(ngx_conf_t *cf)

{

  

}

備註:

將全域變數

flag 清零,用於判斷是否有barrier模組。如果不清零,將會導致如果配置中沒有加入barrier zone,在kill –HUP時,由於全域變數flag值不變,即不為0,指令出現段錯誤。 3HTTP commands

4、常用變數 00      //

指令存放位置#define NGX_HTTP_SRV_CONF   #define NGX_HTTP_LOC_CONF         0x08000000

#define NGX_HTTP_UPS_CONF X_HTTP_SIF_CONF         0x20000000

#define NGX_HTTP_LIF_CONF    MT_CONF         0x800000000 

#define NGX_HTTP_MAIN_CONF_OFFOFF時期與t, loc_conf)

 

範例:

static ngx_command_command_com [] = {

{

ngx_string("print"),

ng 

      ngx_http_print_setup,       

      //set()

      //set()

,會轉換讀入指令傳進來的參數,並將適當的值存到配置結構體

      NGX_HTTP_LOC_CONF_OFFSET,

      NULL

},

 

      ngx_null_command

};

};

5.1 

處理

Request

Request

ngx_http_get_module_main_conf

函數功能:

根據request

#define ngx_http_get_module_main_conf(r, module)           (r)->main_conf[module.ctx_index]

ngx_http_get_module_srv_conf

函數功能: 配置。 #define ngx_http_get_module_srv_conf(r, module)  (r)->srv_conf[module.ctx_index] 函數功能:

根據

request

請求和模組得到location

配置。 #define ngx_http_get_module_loc_conf(r, module)  (r)->loc_conf[module.ctx_index] 解析conf

ngx_http_conf_get_module_main_conf

函數功能:根據conf

#define ngx_http_conf_get_module_main_conf(cf, module)         _http_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]

ngx_http_conf_get_module_srv_conf

函數功能:根據conf結構和模組得到server配置。

#define ngx_http_conf_get_module_srv_conf(cf, module)         (ngx_http_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]

ngx_http_conf_get_module_loc_confngx_http_conf_get_module_loc_conf

函數功能:根據

conf

結構和模組得到

location

配置。

#define ngx_http_conf_get_module_loc_conf(cf, module)         ((ngx_http_conf_ctx_t *) cf->ctx)->loc_conf[module.ctx_index]5.3 Cycle ngx_http_cycle_get_module_main_conf

#define ngx_http_cycle_get_module_main_confࠠ

    (cycle->conf_ctx[ngx_http_module.index] ?              

        ((ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index ])      

            ->main_conf[Hule.ctx_index                  NULL)

    NULL) 

 

 

以上就介紹了nginx HTTP模組組成,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn