解剖Nginx·模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现
还记得我们定义过一个结构体如下吗?
typedef struct {
ngx_str_t output_words;
} ngx_http_hello_world_loc_conf_t;
它就是 HelloWorld 的 location 组件配置,其中有一个字符串成员 output_words。
1 create location
用于 ngx_http_hello_world_module_ctx 中的 location 创建函数:
static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf) {
ngx_http_hello_world_loc_conf_t* conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->output_words.len = 0;
conf->output_words.data = NULL;
return conf;
}
我们可以看到,就是先分配一段 ngx_http_hello_world_loc_conf_t 所使用的大小的内存。并初始化 ngx_http_hello_world_loc_conf_t 唯一的成员 output_words。
2 merge location
用于 ngx_http_hello_world_module_ctx 中的 location 合并函数:
static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf,
void* parent,
void* child) {
ngx_http_hello_world_loc_conf_t* prev = parent;
ngx_http_hello_world_loc_conf_t* conf = child;
ngx_conf_merge_str_value(conf->output_words, prev->output_words, "boy");
return NGX_CONF_OK;
}
3 ngx_http_hello_world
3.1 ngx_http_conf_get_module_loc_conf
首先你要了解一个 Nginx 提供的一个“函数”:
ngx_http_conf_get_module_loc_conf(
cf, // configuration
module
);
实际上它是一个宏定义,在 ngx_http_config.h 中:
#define ngx_http_conf_get_module_loc_conf(cf, module) \
((ngx_http_conf_ctx_t *) cf->ctx)->loc_conf[module.ctx_index]
它的作用是通过 cf 配置的上下文,找到指定的 module 中的 location configuration。
3.2 ngx_http_hello_world
用于 ngx_http_hello_world_commands 中我们定义的唯一的一个命令的 set 字段。
static char* ngx_http_hello_world(ngx_conf_t* cf,
ngx_command_t* cmd,
void* conf) {
ngx_http_core_loc_conf_t* clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_world_handler;
ngx_conf_set_str_slot(cf, cmd, conf);
return NGX_CONF_OK;
}
这个函数的作用,就是生成对请求的响应内容,即本例中的hello_world, Poechant。 然后获取到 http_core_module 的 location configuration,即 clcf(Core Location ConF)。给 clcf 的 handler 字段赋值 ngx_http_hello_world_handler,这个函数下面会介绍。然后再常规地调用 ngx_conf_set_str_slot。
4 ngx_http_hello_world_handler
首先你要再了解一个 Nginx 提供的一个“函数”:
4.1 ngx_http_conf_get_module_loc_conf
ngx_http_conf_get_module_loc_conf(
r, // request
module
);
实际上它是一个宏定义,在 ngx_http_config.h 中:
#define ngx_http_get_module_loc_conf(r, module) (r)->loc_conf[module.ctx_index]
其作用是根据 module 的索引字段(ctx_index),找到 request 所请求的 location 配置。
4.2 ngx_http_hello_world_handler
首先来看看 Nginx 中比较经典的缓冲区 ngx_buf_t 吧。这里只介绍与本文相关的部分。
struct ngx_buf_s {
u_char *pos;
u_char *last;
u_char *start; /* start of buffer */
u_char *end; /* end of buffer */
…
};
这四个指针把缓冲区划分为 3 个部分。分别如下:
- 第一部分(start 到 pos):
- 只读缓冲区:对于只读缓冲区,这部分是已读部分;
- 只写缓冲区:对于只写缓冲区,不会有这部分。
- 第二部分(pos 到 last):
- 只读缓冲区:对于只读缓冲区,这部分是欲读取的部分;
- 只写缓冲区:对于只写缓冲区,已写入的区域。
- 第三部分(last 到 end):
- 只读缓冲区:对于只读缓冲区,不会有这部分;
- 只写缓冲区:对于只写缓冲区,剩余可写区域。
ngx_buf_t 之所以经典的另一个原因,是因为nginx可以提前flush输出,所以这些buf被输出后就可以重复使用,可以避免重分配,提高系统性能,被称为free_buf,而没有被输出的buf就是busy_buf。
那么来看 ngx_http_hello_world_handler 吧:
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t* r) {
ngx_int_t rc;
ngx_buf_t* b;
ngx_chain_t out[2];
ngx_http_hello_world_loc_conf_t* hlcf;
hlcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_world_module);
// 设置 request 的 header
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char*)"text/plain";
// 分配缓冲区的内存空间
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
// 第 1 块缓冲区
out[0].buf = b;
out[0].next = &out[1];
// 本模块中,缓冲区只需要写入数据,所以只设置 pos 和 last
b->pos = (u_char*)"hello_world, ";
b->last = b->pos + sizeof("hello_world, ") - 1;
b->memory = 1; // 标示缓冲区是内存缓冲
// 分配缓冲区的内存空间
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
// 第 2 块缓冲区
out[1].buf = b;
out[1].next = NULL;
// 本模块中,缓冲区只需要写入数据,所以只设置 pos 和 last
b->pos = hlcf->output_words.data;
b->last = hlcf->output_words.data + (hlcf->output_words.len);
b->memory = 1; // 标示缓冲区是内存缓冲
b->last_buf = 1; // 标示整个响应最后一个缓冲区,nginx会立即发送缓冲的所有数据
// 设置 request 的 header
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = hlcf->output_words.len + sizeof("hello_world, ") - 1;
// 发送 request
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out[0]);
}
5 Reference
- www.evanmiller.org/nginx-modules-guide.html
- http://blog.sina.com.cn/s/blog_7303a1dc0100x70t.html
-
解剖Nginx·模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现的更多相关文章
- 解剖Nginx·模块开发篇(2)ngx_http_hello_world_module 模块基本结构定义
elloWorld 是一个典型的 location 模块.什么是 location 模块?在 Nginx 中,根据作用域,有 main 模块.server 模块.location 模块. 1 模块定义 ...
- 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!
1 学习 Nginx 模块开发需要有哪些准备? 需要的预备知识不多,有如下几点: 有过一些 C 语言的编程经历: 知道 Nginx 是干嘛的,并有过编写或改写 Nginx 的配置文件的经历. OK,就 ...
- nginx模块开发篇 (阿里著作)
背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...
- 【Nginx】开发一个HTTP过滤模块
与HTTP处理模块不同.HTTP过滤模块的工作是对发送给用户的HTTP响应做一些加工. server返回的一个响应能够被随意多个HTTP过滤模块以流水线的方式依次处理.HTTP响应分为头部和包体,ng ...
- 解剖Nginx·自动脚本篇(5)编译器相关主脚本
在 Nginx 的自动脚本中,auto/cc目录下的所有脚本都是用于编译器相关配置使用的.Nginx的出色跨平台性(Linux.Darwin.Solaris.Win32 等)就有这些脚本的贡献.该目录 ...
- 解剖Nginx·模块开发篇(5)解读内置非默认模块 ngx_http_stub_status_module
1 Background ngx_http_stub_status_module 是一个 Nginx 的内置 HTTP 模块,该模块可以提供 Nginx 的状态信息.默认情况下这个模块是不被编译进来的 ...
- 解剖Nginx·模块开发篇(4)模块开发中的命名规则和模块加载与运行流程
1 命名规则 1.1 基本变量 基本变量有三个: ngx_module_t 类型的 ngx_http_foo_bar_module: ngx_command_t 类型的数组 ngx_http_foo_ ...
- Nginx模块开发1_明白自定义模块的编译流程
自定义模块的编译流程 --add-module参数 configure使用--add-module参数指定添加模块目录. config脚本 由--add-module指定的目录保存为$ngx-addo ...
- 解剖Nginx·自动脚本篇(3)源码相关变量脚本 auto/sources
在configure脚本中,运行完auto/options和auto/init脚本后,接下来就运行auto/soures脚本.这个脚本是为编译做准备的. 目录 核心模块 事件模块 OpenSSL 模块 ...
随机推荐
- jQuery的deferred对象详解(转载)
jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本.(由于无法转载,复制原文 .原文链接——原作者:阮一峰) 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5. ...
- [LeetCode系列]BST有效性确定问题[前序遍历]
给定一个BST的根节点, 试判断此BST是否为符合规则的BST? 规则: 对于一个BST的节点, 它左侧的所有节点(包括子节点)必须小于它本身; 它右侧的所有节点(包括子节点)必须大于它本身; 它的左 ...
- 微信web端生成支付二维码
授权获取二维码类: <?php /** * Trade类 * @author xyyphp * @date 2016/10/10 */ abstract class TradeControlle ...
- Zookeeper--配置服务
Zookeeper--配置服务 配置服务是分布式应用中重要的服务,作用是使集群中的机器可以共享配置信息中公共的部分.ZooKeeper可作为一个具有高可用,全局一致的配置服务器,允许客户端获取和更新配 ...
- 全局获取Context
1.定制一个Application类,管理全局的状态信息 public class MyApplication extends Application{ private static Context ...
- java web 程序---登陆验证4个页面
思路: 1.第一个是登陆页面login.jsp一个form表单.点击登陆按钮 2.第二个是验证页面check.jsp.如果username和password都正确.则跳转到另一个页面a.jsp显示登陆 ...
- 20165226 2017-2018-4 《Java程序设计》第7周学习总结
20165226 2017-2018-4 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章 JDBC与MySQL数据库 下载MySQL最新版本. 连接数据库 下载JDBC-M ...
- 【原】C++11并行计算 — 数组求和
本文转载请注明出处 -- polobymulberry-博客园 0x00 - 前言 最近想优化ORB-SLAM2,准备使用并行计算来提高其中ORB特征提取的速度.之前对并行计算方面一窍不通.借此机会, ...
- python中scipy学习——随机稀疏矩阵及操作
1.生成随机稀疏矩阵: scipy中生成随机稀疏矩阵的函数如下: scipy.sparse.rand(m,n,density,format,dtype,random_state) 1 参数介绍: 参数 ...
- 反向生成hibernate实体类和映射文件
工欲善其事,必先利其器.我们可以使用IDE来根据数据库中的表反向生成实体类和映射文件,虽然这些东西手写也并不是难度很大,但是如果存在大量的简单工作需要我们做,也会显得很麻烦. 写在前面 我们反向生成的 ...