搜索
首页后端开发php教程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中,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;                                         //返回自定义的结构

}

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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
继续使用PHP:耐力的原因继续使用PHP:耐力的原因Apr 19, 2025 am 12:23 AM

PHP仍然流行的原因是其易用性、灵活性和强大的生态系统。1)易用性和简单语法使其成为初学者的首选。2)与web开发紧密结合,处理HTTP请求和数据库交互出色。3)庞大的生态系统提供了丰富的工具和库。4)活跃的社区和开源性质使其适应新需求和技术趋势。

PHP和Python:探索他们的相似性和差异PHP和Python:探索他们的相似性和差异Apr 19, 2025 am 12:21 AM

PHP和Python都是高层次的编程语言,广泛应用于Web开发、数据处理和自动化任务。1.PHP常用于构建动态网站和内容管理系统,而Python常用于构建Web框架和数据科学。2.PHP使用echo输出内容,Python使用print。3.两者都支持面向对象编程,但语法和关键字不同。4.PHP支持弱类型转换,Python则更严格。5.PHP性能优化包括使用OPcache和异步编程,Python则使用cProfile和异步编程。

PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。