nginx源码配置图解
来源:互联网 发布:张予曦真人好看吗 知乎 编辑:程序博客网 时间:2024/04/27 11:21
nginx配置结构清晰,层次分明,这得益于整个架构的模块化设计,文本将揭示配置文件如何被处理和应用。
整个配置文件解析后的结果如图这样存储。
一、解析的核心机制
nginx源码里,ngx_conf_t是解析的关键结构体
ngx_conf_handler函数里:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* set up the directive's configuration context */
conf = NULL;
/* direct指令,一般是core类型模块指令,比如 daemon, work_processes */
if
(cmd->type & NGX_DIRECT_CONF) {
conf = ((
void
**) cf->ctx)[ngx_modules[i]->index];
/* 直接存储,比如 ngx_core_conf_t */
/* main指令,比如 events, http,此时指向它的地址,这样才能分配数组指针,存储属于它的结构体们。 */
}
else
if
(cmd->type & NGX_MAIN_CONF) {
conf = &(((
void
**) cf->ctx)[ngx_modules[i]->index]);
/* 参考图片 */
}
else
if
(cf->ctx) {
confp = *(
void
**) ((
char
*) cf->ctx + cmd->conf);
/* 有移位的,因此http有三个部分,main, srv, conf,这个就为此而设计的,继续下面的sendfile指令 */
if
(confp) {
conf = confp[ngx_modules[i]->ctx_index];
}
}
rv = cmd->set(cf, cmd, conf);
比如sendfile指令:
{ ngx_string(
"sendfile"
),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
/* 这个对应上面理解,正因有这个offset,它才找到loc部分的配置 */
offsetof(ngx_http_core_loc_conf_t, sendfile),
NULL }
二、配置的应用
1、最简单形式,direct方式
1
2
3
4
5
6
7
/* 定义获取配置文件的宏 */
#define ngx_get_conf(conf_ctx, module) conf_ctx[module.index]
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
if
(ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
ngx_process = NGX_PROCESS_MASTER;
}
2、稍微复杂点形式,main方式
1
2
3
4
5
6
7
8
9
/* 定义获取配置文件的宏,注意指针 */
#define ngx_event_get_conf(conf_ctx, module) \
(*(ngx_get_conf(conf_ctx, ngx_events_module))) [module.ctx_index];
ngx_epoll_conf_t *epcf;
epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module);
nevents = epcf->events;
3、不简单的http配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* 宏定义,r是什么,稍后解释 */
#define ngx_http_get_module_loc_conf(r, module) (r)->loc_conf[module.ctx_index]
ngx_http_log_loc_conf_t *lcf;
lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
...
/* r是请求,它是这么来的,在ngx_http_init_request函数里(ngx_http_request.c文件)*/
r = ...;
cscf = addr_conf->default_server;
r->main_conf = cscf->ctx->main_conf;
r->srv_conf = cscf->ctx->srv_conf;
r->loc_conf = cscf->ctx->loc_conf;
三、重提模块化设计
学着用模块化的角度去看nginx的整体设计,一切以模块为核心,配置依赖于模块,即模块本身就携带着它的配置。正因为这样的设计的,配置文件的解析,使用非常简单。避免过多使用全局变量好像成为一种共识,但是在nginx世界里,全局变量可不少,每个模块都是个全局变量,为什么这样设计呢?因为模块之间是有依赖性的,所以需要互相访问。
配置文件解析这块的代码极具借鉴,本文只管窥般分析了配置文件,不能剥夺读者阅读源码的享受,点到即止。
0 0
- nginx源码配置图解
- nginx源码分析之配置图解
- nginx源码分析之配置图解
- nginx 源码安装配置
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- CentOS+Nginx+PHP+MySQL配置教程(图解)
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- Nginx源码分析—nginx的配置
- [nginx源码分析]配置合并
- nginx-1.8源码安装配置
- 图解Linux下源码安装PHP7.0.9 +Nginx
- Nginx反向代理实现负载均衡配置图解
- Nginx反向代理实现负载均衡配置图解
- Nginx反向代理实现负载均衡配置图解
- 快速简单地求负数的补码和求补码代表的负数
- Android退出程序(二)——利用广播机制
- mongodb集群shard中empty chunk的处理
- XML编码规则整理,值得收藏
- Nginx负载均衡
- nginx源码配置图解
- oozie-4.1.0 安装
- WebView回收资源的报错的解决方案----Error: WebView.destroy() called while still attached!
- Qt 3D的研究(四):指定渲染的材质以及效果
- ajax与后台交互
- 如何查看你的系统是legacy还是UEFI启动
- python学习笔记(1)基础知识
- 100道不错的算法面试题
- javascript深入理解 `闭包`