Home  >  Article  >  Backend Development  >  nginx HTTP module composition

nginx HTTP module composition

WBOY
WBOYOriginal
2016-08-08 09:23:09839browse

Original link: http://cjhust.blog.163.com/blog/static/17582715720124544047608/

1, data structure

ngx_conf_s

struct ngx_conf_s {

char *name;

ngx_array_t *args; // command parameters, read from the file and put into this array

ngx_cycle_t *cycle; //points to system parameters

ngx_pool_t *pool; ngx_pool_t               *temp_pool;

ngx_log_t *log; /Log

void //(void ****)cycle->conf_ctx

, which contains the configuration information of all modules ngx_uint_t module_type;

//

The type of module that handles the current instruction ngx_uint_t cmd_type; //

Process this command Type of command ngx_conf_handler_pt handler; //

Command processing function char                                                                                  Use

handler

with the above ngx_conf_s

acts as a bridge. ngx_http_conf_ctx_t

typedef struct {

    void        **main_conf;

    void        **srv_conf;

    void        **loc_conf;

} ngx_http_conf_ctx_t;

备注:HTTP block中的配置结构主要分为3中,mainserver{}location{}

ngx_http_module_t

typedef struct {

    ngx_int_t   (*preconfiguration)(ngx_conf_t *cf);

    ngx_int_t   (*postconfiguration)(ngx_conf_t *cf);

 

    void       *(*create_main_conf)(ngx_conf_t *cf);

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

 

    void       *(*create_srv_conf)(ngx_conf_t *cf);

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

 

    void       *(*create_loc_conf)(ngx_conf_t *cf);

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

} ngx_http_module_t;

备注:HTTP模块中的ctx主要有上面8个函数组成。

ngx_command_s

struct ngx_command_s {

    ngx_str_t             name;

    ngx_uint_t            type;

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

    ngx_uint_t            conf;

    ngx_uint_t            offset;

    void                 *post;

};

 

#define ngx_null_command  { ngx_null_string, 0, NULL, 0, 0, NULL }

2HTTP ctx

2.1 create_main_conf(ngx_conf_t *cf)

Parameters: ngx_conf_t *cf, configuration structure;

Return value: + gx_http The result of _get_module_ main _conf;

Example: static void * ngx_http_barrier_create_conf(ngx_conf_t *cf) { ngx_http_barrier_conf_t *conf; / /A custom structure

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

if (conf == NULL) {

  return NULL;

} conf->enable = NGX_CONF_UNSET;

return conf; //

Return the custom structure

}

2.2 create_srv_conf(ngx_conf_t *cf)

Parameters: ngx_conf_t *cf, configuration structure;

Return value: void *

;

Note: return value can be used as x_http The result of _get_module_ srv_conf;

Example:

static void * ngx_http_barrier_create_conf(ngx_conf_t *cf)

{ ngx_http_barrier_conf_t *conf; // A customized structure conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_barrier_conf_t)); //init by pcalloc

if (conf == NULL) {

N Return null; }

CONF- & GT; enable = ngx_conf_unset;

Return conf; // Return to the custom structure

}}

2.3 create_loc_conf(ngx_conf_t *cf)

Parameters:

ngx_conf_t *cf

, configuration structure;

Return value: void *

;

Note:

The return value can be used as

ngx_http_conf_get_module_loc_conf and gx_http The result of _get_module_loc_conf;

Example: static void * ngx_http_barrier_create_conf(ngx_conf_t *cf)

{ ngx_http_barrier_conf_t *conf; // A customized structure

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

if (conf == NULL) {

return NULL;

}

conf->enable = NGX_CONF_UNSET;

return conf; //Return to custom The structure of

}

2.4 preconfiguration(ngx_conf_t *cf )

Parameters: ngx_conf_t *cf, configuration structure;

Return value: ngx_int_t

Remarks: The return value is only used as a judgment whether the operation is correct;

Example:

static ngx_int_t

ngx_http_ssl_add _variables(ngx_conf_t *cf)

{

ngx_http_variable_t *var, *v;

for (v = ngx_http_ssl_vars; v->name.len; v++) {

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

if (var == NULL) {

var->get_handler = v-> ;get_handler;

var->data = v->data;

}

return NGX_OK;

}

2.5 init_main_conf(ngx_conf_t *cf, void *conf)

Parameters: ngx_conf_t *cf

and conf, conf are create_main The return value, here as a parameter; Return value: char *

, NGX_CONF_OK indicates success; Remarks: The return value is only used as a judgment whether the operation is correct;

Example:

static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void * conf)

{

 ngx_http_core_main_conf_t *cmcf = conf;

//create_main

is not configured

if (cmcf->server_names_hash _max_size == NGX_CONF_UNSET_UINT) {

   cmcf ->server_names_hash_max_size = 512; //

Here is

init

}

if (cmcf ->server_names_hash_bucket_size == NGX_CONF_UNSET_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_hash_max_size == NGX_CONF_UNSET_UINT) {

        cmcf->variables_hash_max_size = 512;

    }

 

    if (cmcf->variables_hash_bucket_size == NGX_CONF_UNSET_UINT) {

        cmcf->variables_hash_bucket_size = 64;

    }

 

    cmcf->variables_hash_bucket_size =

               ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);

 

    if (cmcf->ncaptures) {

        cmcf->ncaptures = (cmcf->ncaptures + 1) * 3;

    }

 

    return NGX_CONF_OK;

}

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

参数:ngx_conf_t *cf,配置结构体,prevmain的配置结构体,confserver的配置结构体;prev=cf->ctx.srv_conf[ctx_index],c/span>

返回值:char *,正确返回值是NGX_CONF_OK

备注:返回值只是作为操作是否正确的一个判断;

示例:

static char * ngx_http_barrier_merge_conf(ngx_conf_t *cf, void *parent, void *child)

{

  ngx_http_barrier_conf_t *prev = parent;

  ngx_http_barrier_conf_t *conf = child;

 

  if (conf->shm_zone == NULL){

     *conf = *prev;

  }

 

  ngx_conf_merge_value(conf->enable, prev->enable, 0);  //default is 0

  return NGX_CONF_OK;

}

备注:merge server的主要功能是,如果main里配置了enable=1,而server{}enable= NGX_CONF_UNSET,则将serverenable=mainenable=1

2.7 merge_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_conf_t *cf

返回值:ngx_int_t,正确返回值是NGX_OK

备注:返回值只是作为操作是否正确的一个判断;

示例:

static ngx_int_t ngx_http_tracker_init(ngx_conf_t *cf)

{

ngx_tracker_flag = 0;

return NGX_OK;

}

Note: set the global variable flag Cleared to determine whether there is a barrier module. If it is not cleared, it will result in barrier if it is not added in the configuration. zone, when kill –HUP, because the value of the global variable flag does not change, that is, it is not 0, when the user executes the traker command, A segfault occurred.

3HTTP commands

4, commonly used variables

#define NGX_HTTP_MAIN_CONF                                                                                                                                                             Command storage location

#define NGX_HTTP_SRV_CONF    0x04000000

#define NGX_HTTP_LOC_CONF 0x08000000

#define NGX_HTTP_UPS_CONF 0x10000000

#define NGX_HTTP_SIF_CONF 0x20000000

#define NGX_HTTP_LIF_CONF 0x40000000

#define NGX_HTTP_LMT_CONF 0x80000000

#define NGX_HTTP_MAIN_CONF_OFFSET offsetof(ngx_http_conf_ctx_t , main_conf)

#define NGX_HTTP_SRV_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, srv_conf)

#define NGX_HTTP_LOC_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, loc_ conf)

Example:

static ngx_command_t ngx_http_print_commands [] = {

{

ngx_string("print"),

  NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,

ngx_http_ print_setup,​​​​​​​​

​​​​ //set(), will convert to read Input the parameters passed in the command and save the appropriate value to the configuration structure

NGX_HTTP_LOC_CONF_OFFSET,

offsetof(ngx_http_print_loc_conf_t, ed),

        NULL

},

ngx_null_command

};

5, commonly used functions 5.1

ProcessingRequest ngx_http_get_module_main_conf

Function function:

According to

request request and module get main configuration. #define ngx_http_get_module_main_conf(r, module)                                                            

ngx_http_get_module_srv_conf

Function function: According to request request and module get server Configuration.

#define ngx_http_get_module_srv_conf(r, module) (r)->srv_conf[module.ctx_index]

ngx_http_get_module_loc_conf

Function function: According to requestRequests and modules get location configured.

#define ngx_http_get_module_loc_conf(r, module) (r)->loc_conf[module.ctx_index]

5.2 conf

ngx_http_conf_get_module_main_conf

Function function: Get main configuration based on the conf structure and module.

#define ngx_http_conf_get_module_main_conf(cf, module)                                                               main_conf[module.ctx_index]

ngx_http_conf_get_module_srv_conf

Function:

is configured according to the conf structure and module. #define ngx_http_conf_get_module_srv_conf(cf, module)                                              gt;srv_conf[module.ctx_index]ngx_http_conf_get_module_loc_conf

Function: is configured according to the

conf structure and module.

#define ngx_http_conf_get_module_loc_conf(cf, module)                                                           t;loc_conf[module.ctx_index]5.3 Cycle ngx_http_cycle_get_module_main_conf

#define ngx_http_cycle_get_module_main_conf(cycle, module) (cycle->conf_ctx [ngx_http_module.index] ? [ngx_http_module.index])                                                                                            NULL)

The above introduces the composition of the nginx HTTP module, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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