nginx HTTP模块组成
原文链接:http://cjhust.blog.163.com/blog/static/17582715720124544047608/
1、数据结构
ngx_conf_s
ngx_array_t *args; //指令参数,从文件读入并放入这个数组
ngx_cycle_t *cycle; //指向系统参数
ngx_pool_t *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
备注:HTTP block中的配置结构主要分为3中,main、server{}、location{}。
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教程有兴趣的朋友有所帮助。

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

Windows上的激活过程有时会突然转向显示包含此错误代码0xc004f069的错误消息。虽然激活过程已经联机,但一些运行WindowsServer的旧系统可能会遇到此问题。通过这些初步检查,如果这些检查不能帮助您激活系统,请跳转到主要解决方案以解决问题。解决方法–关闭错误消息和激活窗口。然后,重新启动计算机。再次从头开始重试Windows激活过程。修复1–从终端激活从cmd终端激活WindowsServerEdition系统。阶段–1检查Windows服务器版本您必须检查您使用的是哪种类型的W
