您好,欢迎访问一九零五行业门户网

nginx HTTP模块组成

原文链接:http://cjhust.blog.163.com/blog/static/17582715720124544047608/
1、数据结构
ngx_conf_s
struct ngx_conf_s {
    char                 *name; 
    ngx_array_t          *args;             //指令参数,从文件读入并放入这个数组
ngx_cycle_t          *cycle;            //指向系统参数
    ngx_pool_t           *pool;              //内存池
    ngx_pool_t           *temp_pool;
    ngx_conf_file_t      *conf_file;      //配置文件信息 “./conf/main.conf”
    ngx_log_t            *log;                 //日志
void                 *ctx;                    //(void ****)cycle->conf_ctx,装的是所有模块的配置信息
    ngx_uint_t            module_type;  //处理当前指令的模块的类型
    ngx_uint_t            cmd_type;       //处理这个指令的命令的类型
ngx_conf_handler_pt   handler;     //指令处理函数
    char                 *handler_conf;     //这个是配合上面的handler使用
};
备注:该结构体主要用于读取配置时候,从配置文件和系统参数之间,ngx_conf_s起着桥梁的作用。
ngx_http_conf_ctx_t
typedef struct {
    void        **main_conf;
    void        **srv_conf;
    void        **loc_conf;
} ngx_http_conf_ctx_t;
备注:http block中的配置结构主要分为3中,main、server{}、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 }
2、http ctx
2.1 create_main_conf(ngx_conf_t *cf)
参数:ngx_conf_t *cf,配置结构体;
返回值:void *;
备注:返回值可以作为ngx_http_conf_get_module_main_conf和gx_http _get_module_ main _conf的结果;
示例:
static void * ngx_http_barrier_create_conf(ngx_conf_t *cf)
{
  ngx_http_barrier_conf_t  *conf;   //自定义的一个结构
  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;                                         //返回自定义的结构
}
2.2 create_srv_conf(ngx_conf_t *cf)
参数:ngx_conf_t *cf,配置结构体;
返回值:void *;
备注:返回值可以作为ngx_http_conf_get_module_srv_conf和gx_http _get_module_ srv_conf的结果;
示例:
static void * ngx_http_barrier_create_conf(ngx_conf_t *cf)
{
  ngx_http_barrier_conf_t  *conf;   //自定义的一个结构
  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;                                        //返回自定义的结构
}
2.3 create_loc_conf(ngx_conf_t *cf)
参数:ngx_conf_t *cf,配置结构体;
返回值:void *;
备注:返回值可以作为ngx_http_conf_get_module_loc_conf和gx_http _get_module_loc_conf的结果;
示例:
static void * ngx_http_barrier_create_conf(ngx_conf_t *cf)
{
  ngx_http_barrier_conf_t  *conf;   //自定义的一个结构
  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;                                        //返回自定义的结构
}
2.4 preconfiguration(ngx_conf_t *cf)
参数:ngx_conf_t *cf,配置结构体;
返回值:ngx_int_t;
备注:返回值只是作为操作是否正确的一个判断;
示例:
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) {
            return ngx_error;
        }
var->get_handler = v->get_handler;
        var->data = v->data;
    }
return ngx_ok;
}
2.5 init_main_conf(ngx_conf_t *cf, void *conf)
参数:ngx_conf_t *cf和conf,conf是create_main时的返回值,在这里作为参数;
返回值:char *,ngx_conf_ok表示成功;
备注:返回值只是作为操作是否正确的一个判断;
示例:
static char *
ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
{
    ngx_http_core_main_conf_t *cmcf = conf;
//create_main时是未配置
if (cmcf->server_names_hash_max_size == ngx_conf_unset_uint) {
        cmcf->server_names_hash_max_size = 512;    //这里是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,配置结构体,prev是main的配置结构体,conf是server的配置结构体;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,则将server的enable=main的enable=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;
}
备注:将全局变量flag清零,用于判断是否有barrier模块。如果不清零,将会导致如果配置中没有添加barrier zone,在kill –hup时,由于全局变量flag值不变,即不为0, 用户在执行traker指令时,出现段错误。
3、http commands
4、常用变量
#define ngx_http_main_conf        0x02000000      //指令存放位置
#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)
示例:
static ngx_command_t  ngx_http_print_commands[] = {
{
ngx_string(print),
      ngx_http_loc_conf|ngx_conf_take1,    
      ngx_http_print_setup,       
      //set(),会转化读入指令传进来的参数,并将合适的值保存到配置结构体
      ngx_http_loc_conf_offset,
      offsetof(ngx_http_print_loc_conf_t, ed),
      null
},
ngx_null_command
};
5、常用函数
5.1 处理request
ngx_http_get_module_main_conf函数功能:根据request请求和模块得到main配置。
#define ngx_http_get_module_main_conf(r, module)                             \
(r)->main_conf[module.ctx_index]
ngx_http_get_module_srv_conf函数功能:根据request请求和模块得到server配置。
#define ngx_http_get_module_srv_conf(r, module)  (r)->srv_conf[module.ctx_index]
ngx_http_get_module_loc_conf函数功能:根据request请求和模块得到location配置。
#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函数功能:根据conf结构和模块得到main配置。
#define ngx_http_conf_get_module_main_conf(cf, module)                        \
((ngx_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_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, module)                    \
    (cycle->conf_ctx[ngx_http_module.index] ?                                 \
        ((ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index])      \
            ->main_conf[module.ctx_index]:                                    \
        null)
以上就介绍了nginx http模块组成,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息