Nginx开发HTTP模块入门

我们以一个最简单的Hello World模块为例,学习Nginx的模块编写。假设我们的模块在nginx配置文件中的指令名称为hello_world,那我们就可以在nginx.conf文件中配置这个指令

location / {
hello_world;
}

这样,当我们访问首页的时候就会执行hello_world指令,输出Hello World。接下来,就开始编写我们的Hello World模块,按照nginx命名规则,我们把这个模块取名为ngx_http_hello_world_module

编写config文件

我们先建一个目录hello_world来保存我们的扩展

mkdir /opt/nginx/ext/hello_world

要想一个办法把我们的模块编译到nginx中,我们需要在执行./configure命令的时候加入--add-module=PATH,这里的PATH就是我们扩展目录,所以我们要完成./configure需要使用的config文件,在扩展目录下,新建config文件,

vim /opt/nginx/ext/hello_world/config

内容如下

ngx_addon_name=ngx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
CORE_LIBS="$CORE_LIBS -lpcre"
  1. ngx_addon_name:仅在configure执行时使用,一般设置为模块名称
  2. HTTP_MODULES:保存所有的HTTP模块名称,每个HTTP模块间由空格符相连,在重新设置HTTP_MODULES变量时,不要直接覆盖他,应该使用空格追加模块
  3. NGX_ADDON_SRCS:指定扩展模块的源代码

编写模块文件

编写好了config文件之后,就可以编写config文件中定义的模块文件了,新建模块文件

vim /opt/nginx/ext/hello_world/ngx_http_hello_world_module.c

定义HTTP模块

在nginx中,模块的数据类型是ngx_module_t

这个类型里面的成员详细可以参考源码,我们直接写出ngx_http_hello_world_module模块定义

ngx_module_t  ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx, /* module context */
ngx_http_hello_world_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
  1. NGX_MODULE_V1:表示版本号,使用内置的宏定义即可
  2. ngx_http_hello_world_module_ctx:这个成员很重要,设置上下文结构,HTTP框架要求,它必须指向ngx_http_module_t接口
  3. ngx_http_hello_world_commands:定义模块的配置文件参数,这个会作用在nginx.conf文件解析
  4. NGX_HTTP_MODULE:表示这是一个HTTP模块

编写模块上下文结构

这个结构体针对本文的HTTP模块来说,不需要调用,设置为NULL即可,但是HTTP框架要求,它必须指向ngx_http_module_t接口,所以我们这样定义

static ngx_http_module_t  ngx_http_hello_world_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};

编写命令结构

ngx_http_hello_world_commands数组用于定义模块的配置文件参数,每一个元素都是ngx_command_t类型,数组的结尾用ngx_null_command表示。我们编写的模块的命令结构如下

static ngx_command_t  ngx_http_hello_world_commands[] = {
{ ngx_string("hello_world"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hello_world,
0,
0,
NULL },
ngx_null_command
}
  1. hello_world:表示我们的配置项名称,和gzip一样,设置名称而已
  2. NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS:指定出现的位置,可以出现在location{}块中
  3. ngx_http_hello_world:这是ngx_command_t中的set成员,当某块配置中出现hello_world时候,nginx将会启动ngx_http_hello_world方法,我们需要实现该函数

编写触发回调函数

前面编写完命令结构,定义了出现hello_world配置项的时候触发ngx_http_hello_world方法,我们要实现这个方法

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;
return NGX_CONF_OK;
}
  1. ngx_http_conf_get_module_loc_conf:这个函数会首先找到hello_world配置项所属的配置块,然后赋值给变量clcf
  2. clcf->handler:HTTP框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时,如果请求的主机域名,URL和hello_wrold相匹配,那么就调用这handler指向的方法ngx_http_hello_world_handler

编写对http请求的具体处理方法hander

前面分析,当匹配URL之后,真正执行的其实是我们的ngx_http_hello_world_handler函数,所以,我们要实现这个函数

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
ngx_int_t rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
} ngx_str_t type = ngx_string("text/plain");
ngx_str_t response = ngx_string("Hello World");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type; rc = ngx_http_send_header(r);//发送头部
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
} ngx_buf_t *b;
b = ngx_create_temp_buf(r->pool, response.len);//异步发送,要用堆内存空间
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
} ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos + response.len;
b->last_buf = 1; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);//向用户发送响应包
}

这个函数的参数是ngx_http_request_t类型参数r,它包含了很多东西(方法,URI,协议,头部等)。

完整的例子

将上面的步骤联系起来,可以最终合并成我们的新模块文件ngx_http_hello_world_module.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_command_t ngx_http_mytest_commands[] = {
{
ngx_string("hello_world"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
ngx_http_hello_world,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
}; static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
}; ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static char *
ngx_http_mytest(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; return NGX_CONF_OK;
} static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_int_t rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
} ngx_str_t type = ngx_string("text/plain");
ngx_str_t response = ngx_string("Hello World");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type; rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
} ngx_buf_t *b;
b = ngx_create_temp_buf(r->pool, response.len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
} ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos + response.len;
b->last_buf = 1; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);
}

编译HTTP模块

经过上面的步骤,我们的扩展模块下面有两个文件在/opt/nginx/ext/hello_world目录中

shell> ls /opt/nginx/ext/hello_world
config
ngx_http_hello_world_module.c

我们使用测试的nginx版本为nginx-1.0.10。在安装过程中,执行./configure的时候需要加入我们模块路径

shell> ./configure --add-module=/opt/nginx/ext/hello_world/nginx_hello_world/

然后进行安装即可

shell> make && make install

测试HTTP模块

修改nginx配置文件nginx.conf,如果不想配置规则,可以直接让访问首页的时候就执行hello_world执行

location / {
hello_world;
}

然后启动我们的nginx

shell> ./sbin/nginx

测试我们的nginx扩展的HTTP模块

shell> curl "127.0.0.1"
Hello World

Nginx开发HTTP模块入门的更多相关文章

  1. nginx开发_Filter模块执行顺序

    Filter模块执行顺序 Filter模块的执行顺序是在执行configure文件时决定的,configure文件执行完成后生成objs/ngx_modules.c,文件中定义了一个数组ngx_mod ...

  2. Nginx开发从入门到精通 学习目录分享学习 (阿里著作)

    Nginx开发从入门到精通   缘起 nginx由于出色的性能,在世界范围内受到了越来越多人的关注,在淘宝内部它更是被广泛的使用,众多的开发以及运维同学都迫切的想要了解nginx模块的开发以及它的内部 ...

  3. 转:Nginx国人开发缩略图模块(ngx_image_thumb)

    ngx_image_thumb是nginx中用来生成缩略图的模块,生存缩略图的方法很多,之前也写过一篇 <nginx生成缩略图配置>,在github上发现国人开发的一款模块,作者的文档写的 ...

  4. nginx健康检查模块源码分析

    nginx健康检查模块 本文所说的nginx健康检查模块是指nginx_upstream_check_module模块.nginx_upstream_check_module模块是Taobao定制的用 ...

  5. Nginx SPDY Pagespeed模块编译——加速网站载入

    在看<Web性能权威指南>的时候,看到了SPDY这货,于是便开始折腾起了这个了,也顺便把pagespeed加了进去. Nginx SPDY 引自百科~~ SPDY(读作“SPeeDY”)是 ...

  6. 【转】前端Web开发MVC模式-入门示例

    前端Web开发MVC模式-入门示例 MVC概论起初来之桌面应用开发.其实java的structs框架最能体现MVC框架:model模型是理解成服务器端的模块程序:view为发送给客服端的内容:cont ...

  7. nginx开发_ngx_palloc源码解析

    功能简介 ngx_pool_t是nginx开发中最经常使用到的内存容器.对动态内存的封装,由框架进行创建与释放,模块开发过程中仅需要进行内存申请,不需要关注何时释放.常见的pool对象有: 1. ng ...

  8. nginx开发_配置项

    nginx开发笔记_配置项 模块的配置项即nginx.conf中的指令,HTTP模块指令可以分为三个级别: main级,直接写在http{}块中的指令 server级,写在server{}块中的指令 ...

  9. Windows 编译安装 nginx 服务器 + rtmp 模块

    有关博客: <Windows 编译安装 nginx 服务器 + rtmp 模块>.<Ubuntu 编译安装 nginx>.<Arm-Linux 移植 Nginx> ...

随机推荐

  1. NOIP2005普及组第3题 采药 (背包问题)

    NOIP2005普及组第3题 采药 时间限制: 1 Sec  内存限制: 128 MB提交: 50  解决: 23[提交][状态][讨论版][命题人:外部导入] 题目描述 辰辰是个天资聪颖的孩子,他的 ...

  2. 网络监控之一:netstat命令

    netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...

  3. 1130 Infix Expression

    题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...

  4. MySql主从复制原理和环境配置搭建

    主从复制原理 实质就是通过二进制的sql文件实现主从复制 MySQL的主从复制是MySQL本身自带的一个功能,不需要额外的第三方软件就可以实现,其复制功能并不是copy文件来实现的,而是借助binlo ...

  5. Android 从本地图库或拍照后裁剪图片并设置头像

    在QQ和微信等应用都会有设置头像,一般都是从本地图库选取或相机拍照,然后再截图自己喜欢的部分,然后设置.最后一步把截取好的图片再保存到本地,来保存头像.为了大家使用方便,我把自己完整的代码贴出来,大家 ...

  6. leetcode696

    本题先寻找字符串中0变1,或者1变0的位置作为分隔位置.然后从这个分隔位置同时向左.右两侧搜索. 找到的左连续串和右连续串,都进行累计. public class Solution { public ...

  7. leetcode318

    public class Solution { public int MaxProduct(string[] words) { ) { ; } int len = words.Length; int[ ...

  8. KinderEditor编辑器 在Asp.Net中的使用

    KinderEditor编辑器的使用 分为简单的三步. 1:添加引用部分 <script src="/KinderEditor/kindeditor-min.js">& ...

  9. 微信小程序中在页面中实现下拉刷新显示提醒语后在消失

    最近在做小程序的时候遇见一个问题,就是页面要下拉刷新给客户一个提醒语,查看了小程序的官方文档 这里有个注意点:如果你是一页进行下拉刷新就在那个文件夹的json里面加上"enablePullD ...

  10. Docker学习笔记_安装和使用nginx

    一.软件环境 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu 18.04,虚拟机IP:192.168.8.25 3.Docker安装在虚拟机Ubuntu 18.04上 二.安装过程 ...