0

0

nginx HTTP模块组成

php中文网

php中文网

发布时间:2016-08-08 09:23:09

|

983人浏览过

|

来源于php中文网

原创

原文链接:http://cjhust.blog.163.com/blog/static/17582715720124124544047608/

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中,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)

参数:ngx_conf_t *cf,配置结构体;

返回值:void *

备注:返回值可以作为ngx_http_conf_get_module_main_confgx_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;                                         //返回自定义的结构

}

Ke361开源淘宝客系统
Ke361开源淘宝客系统

Ke361是一个开源的淘宝客系统,基于最新的ThinkPHP3.2版本开发,提供更方便、更安全的WEB应用开发体验,采用了全新的架构设计和命名空间机制, 融合了模块化、驱动化和插件化的设计理念于一体,以帮助想做淘宝客而技术水平不高的朋友。突破了传统淘宝客程序对自动采集商品收费的模式,该程序的自动 采集模块对于所有人开放,代码不加密,方便大家修改。集成淘点金组件,自动转换淘宝链接为淘宝客推广链接。K

下载

2.2 create_srv_conf(ngx_conf_t *cf)

参数:ngx_conf_t *cf,配置结构体;

返回值:void *

备注:返回值可以作为ngx_http_conf_get_module_srv_confgx_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_confgx_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 *cfconfconfcreate_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,配置结构体,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;

}

备注:将全局变量flag清零,用于判断是否有barrier模块。如果不清零,将会导致如果配置中没有添加barrier zone,在kill –HUP时,由于全局变量flag值不变,即不为0 用户在执行traker指令时,出现段错误。

3HTTP 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教程有兴趣的朋友有所帮助。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
TypeScript+Vue3.0实战教程
TypeScript+Vue3.0实战教程

共122课时 | 30.4万人学习

TypeScript深入讲解—贪吃蛇实战
TypeScript深入讲解—贪吃蛇实战

共30课时 | 2.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号