0

0

nginx 源码(4)主流程

php中文网

php中文网

发布时间:2016-08-08 09:27:59

|

1226人浏览过

|

来源于php中文网

原创

接上文。

nginx以单进程在console运行起来,阅读core/nginx.c中的main方法,前面都是初始化的代码,先不仔细看,容易陷入细致魔戒。直接找到ngx_single_process_cycle的定义,在os/unix/ngx_process_cycle.c中。

在第一个for循环中添加如下代码:

for (i = 0; ngx_modules[i]; i++) {
        char * p = NULL;
        if (ngx_modules[i]->commands != NULL)
        {
            p = ngx_modules[i]->commands->name.data;
        }
        if (ngx_modules[i]->init_process) {
            if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
                /* fatal */exit(2);
            }
            printf("[ngx_process_cycle] module ctx_index=%d index=%d name=%s init process\n", ngx_modules[i]->ctx_index, ngx_modules[i]->index, p);
        }
        else
        {
            printf("[ngx_process_cycle] module ctx_index=%d index=%d name=%s\n", ngx_modules[i]->ctx_index, ngx_modules[i]->index, p);
        }
    }
    printf("[ngx_process_cycle] for ngx_modules init done\n");

作用是打印当前nginx中有哪些模块和相关的索引、名字。

在第二个for循环中,主要就是ngx_process_events方法,查找发现这是一个宏,然后这个宏又是一个全局变量,因为我们加载的epoll模块,所以在event/modules/ngx_epoll_module.c
中找到代码:

ngx_event_actions = ngx_epoll_module_ctx.actions;

发现ngx_process_events方法其实就是ngx_epoll_module_ctx模块中的actions中的ngx_epoll_process_events方法。在这个方法中找到epoll_wait方法被调用的地方,加上如下代码:

printf("[ngx_epoll_module] epoll wait\n");
    events = epoll_wait(ep, event_list, nevents, timer);
    printf("[ngx_epoll_module] epoll wait ---\n");

重新make运行结果:

[main] to start ngx_single_process_cycle [ngx_process_cycle] module ctx_index=0 index=0 name=daemon [ngx_process_cycle] module ctx_index=0 index=1 name=error_log [ngx_process_cycle] module ctx_index=0 index=2 name=include [ngx_process_cycle] module ctx_index=0 index=3 name=events [ngx_process_cycle] module ctx_index=0 index=4 name=connections init process [ngx_process_cycle] module ctx_index=1 index=5 name=rtsig_signo [ngx_process_cycle] module ctx_index=2 index=6 name=epoll_events [ngx_process_cycle] module ctx_index=0 index=7 name=http [ngx_process_cycle] module ctx_index=0 index=8 name=server [ngx_process_cycle] module ctx_index=1 index=9 name=log_format [ngx_process_cycle] module ctx_index=2 index=10 name=(null) [ngx_process_cycle] module ctx_index=3 index=11 name=index [ngx_process_cycle] module ctx_index=4 index=12 name=allow [ngx_process_cycle] module ctx_index=5 index=13 name=rewrite [ngx_process_cycle] module ctx_index=6 index=14 name=proxy_pass [ngx_process_cycle] module ctx_index=7 index=15 name=(null) [ngx_process_cycle] module ctx_index=8 index=16 name=(null) [ngx_process_cycle] module ctx_index=9 index=17 name=(null) [ngx_process_cycle] module ctx_index=10 index=18 name=(null) [ngx_process_cycle] module ctx_index=11 index=19 name=gzip [ngx_process_cycle] module ctx_index=12 index=20 name=charset_map [ngx_process_cycle] module ctx_index=13 index=21 name=userid [ngx_process_cycle] module ctx_index=14 index=22 name=expires [ngx_process_cycle] module ctx_index=15 index=23 name=output_buffers [ngx_process_cycle] module ctx_index=16 index=24 name=(null) [ngx_process_cycle] module ctx_index=17 index=25 name=(null) [ngx_process_cycle] for ngx_modules init done [ngx_epoll_module] epoll wait ^C[ngx_epoll_module] epoll wait —

发现共有25个模块,只有connections模块有init_process方法,还有一些模块name为空。最后程序进入到epoll模块,阻塞在epoll_wait调用处,CTRL+C推出后才执行了后面的打印语句。

这里可以仔细看一下nginx对模块的定义, 看代码的时候容易发现nginx里实际用的类型都以_t结尾,而如果这个类型定义的时候如果是结构体则实际类型都以_s结尾,nginx_module_s的定义在core/ngx_conf_file.h文件中:

网趣购物系统加强升级版
网趣购物系统加强升级版

新版本程序更新主要体现在:完美整合BBS论坛程序,用户只须注册一个帐号,即可全站通用!采用目前流行的Flash滚动切换广告 变换形式多样,受人喜爱!在原有提供的5种在线支付基础上增加北京云网支付!对留言本重新进行编排,加入留言验证码,后台有留言审核开关对购物系统的前台进行了一处安全更新。在原有文字友情链接基础上,增加LOGO友情链接功能强大的6种在线支付方式可选,自由切换。对新闻列表进行了调整,

下载
struct ngx_module_s {
    ngx_uint_t       ctx_index;
    ngx_uint_t       index;
    void            *ctx;
    ngx_command_t   *commands;
    ngx_uint_t       type;
    ngx_int_t      (*init_module)(ngx_cycle_t *cycle);
    ngx_int_t      (*init_process)(ngx_cycle_t *cycle);
#if 0
    ngx_int_t      (*init_thread)(ngx_cycle_t *cycle);
#endif
};

其中有两个索引号,还有命令和3个函数,具体什么含义以后用到再看。

还有另外两个结构体ngx_event_module_t和ngx_event_actions_t; 后者是前者的成员;前者就是nginx事件模块,但是抽象的,具体到某个平台上会有不同的类型,此处为epoll。
ngx_event_module_t如下:

typedef struct {
    ngx_str_t              *name;

    void                 *(*create_conf)(ngx_cycle_t *cycle);
    char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);

    ngx_event_actions_t     actions;
} ngx_event_module_t;

epoll实例如下:

ngx_event_module_t  ngx_epoll_module_ctx = {
    &epoll_name,
    ngx_epoll_create_conf,               /* create configuration */
    ngx_epoll_init_conf,                 /* init configuration */    {
        ngx_epoll_add_event,             /* add an event */
        ngx_epoll_del_event,             /* delete an event */
        ngx_epoll_add_event,             /* enable an event */
        ngx_epoll_del_event,             /* disable an event */
        ngx_epoll_add_connection,        /* add an connection */
        ngx_epoll_del_connection,        /* delete an connection */NULL,                            /* process the changes */
        ngx_epoll_process_events,        /* process the events */
        ngx_epoll_init,                  /* init the events */
        ngx_epoll_done,                  /* done the events */
    }
};

正好ngx_event_module_t中的actions成员在ngx_epoll_module_ctx中定义为与epoll相关的一些函数,而前面的ngx_process_events则是由下面的两行代码串联起来的:
event/ngx_event.h中

#define ngx_process_events   ngx_event_actions.process_events

event/modules/ngx_epoll_module.c中

   ngx_event_actions = ngx_epoll_module_ctx.actions;

ngx_event_actions作为全局变量定义在event/ngx_event.c中:

ngx_event_actions_t               ngx_event_actions;

接下来看一下epoll在nginx中的使用。

to be continued …

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

以上就介绍了nginx 源码(4)主流程,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热门AI工具

更多
DeepSeek
DeepSeek

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

豆包大模型
豆包大模型

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

通义千问
通义千问

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

腾讯元宝
腾讯元宝

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

文心一言
文心一言

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

讯飞写作
讯飞写作

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

即梦AI
即梦AI

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

ChatGPT
ChatGPT

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

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

1142

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

371

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

245

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

37

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

114

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

77

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

863

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

123

2026.02.12

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
走进 ES6 新标准语法
走进 ES6 新标准语法

共15课时 | 1.6万人学习

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

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