解剖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
-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant
-
分享到:
相关推荐
lua-upstream-nginx-module, Nginx C 模块将Lua向ngx_lua公开,用于 Nginx upstreams 电子邮件名称ngx_http_lua_upstream - Nginx MODULE,用于向 Nginx upstreams公开Lua到 ngx_lua目录NAME状态概要说明函数get_...
备用nginx代理模块
二. ngx_http_limit_conn_module指令解释limit_conn_zone语法: limit_conn_zone $variable zo
ngx_http_dav_ext_module.so centos7 nginx 1.18 可以作为模块加载
用于nginx的http正向代理https使用的模块
ngx_cache_purge 是 nginx 模块,此模块可以清理 nginx 的 FastCGI、proxy、 SCGI 和 uWSGI 的缓存。配置指令(相同位置语法)fastcgi_cache_purgesyntax: fastcgi_cache_purge on|off|<method> [from all|<ip> [.....
ngx_sqlite 是嵌入 sqlite 数据库的 nginx 模块。通过强大的nginx server,可以使用http协议访问sqlite数据库。环境- sqlite 3- nginx-1.6.3 安装```sh $ git clone https://github.com/rryqszq4/ngx_sqlite.git...
nginx1.20.2
ngx_http_enhance_mp4_module 该模块是原始 nginx mp4 模块(附带在 nginx 源代码中)的增强版本。 请注意,这是我自己的项目,除了需要ngx_http_mp4_module所有源代码外,它与 nginx 的源代码没有任何关系。 该模块...
ngx_python 是为 nginx 嵌入 python 脚本的模块。环境- python 2.7.*- nginx-1.6.3 安装```sh git clone https://github.com/rryqszq4/ngx_python.git wget 'http://nginx.org/download/nginx-1.6.3.tar.gz' ...
nginx lua module for windows 详细请访问 http://blog.csdn.net/ngwsx/
ngx_realtime_request是nginx用来统计虚拟主机流量的模块, 首先和大家说下这个模块是基于域名的,将会记录这个域名的请求量、发送字节、返回http状态码的数量,特性如下: 1、基于域名记录 2、记录请求数据量 3、...
ngx_http_hls_module Nginx HLS 模块,兼容直播和点播服务.
当Nginx作为文件下载服务的反向代理,用户请求一个非常大的文件的时候,它会一直占满反向代理服务器与后端主机之间的带宽。因为nginx一次获取整个...Configure nginx with--add-module=/path/to/ngx_http_subrange_module
Nginx:一致性哈希(第三方模块ngx_http_consistent_hash):ngx_http_consistent_hash-master.zip
、limit_conn_module、ngx_http_limit_req_module、ngx_http_access_module、ngx_http_auth_basic_module、ngx_http_fastcgi_module、ngx_http_gzip_module、ngx_http_proxy_module、ngx_http_upstream_module、ngx_...
使用nginx作为http/https正向代理ipm包,包含ngx_http_proxy_connect_module 模块,附带了第三方图片代理配置,带有缓存,可直接做图片服务器 下载后执行:rpm -ivh nginx-1.12.2-1.el7_4.ngx.x86_64.rpm 打包教程:...
stream-echo-nginx-module, 用于 Nginx的tcp/流回显模块( ngx_http_echo_module的端口) 电子邮件名称ngx_stream_echo - Nginx/流回波模块( ngx_http_echo模块的端口)目录名称版本概要说明示例 1示例 2示例 3示例 4...
此nginx模块可直接生成验证码和验证验证码,可在nginx配置中自定义验证码大小、长度、字体、过期时间等。此项目无需版权,可自由下载使用或二次开发。 安装方法: 1. $ cp ngx_...