Nginx_开发一个简单的HTTP模块

原创:https://blog.csdn.net/ndzjx/article/details/89420927

 

ngx_list_t:存储数组的链表。操作函数:

ngx_list_create/ngx_list_init/ngx_list_push

 

ngx_table_elt_t:键值对

 

ngx_buf_t:处理大数据的关键数据结构

 

将自己的HTTP模块编译进Nginx:

1:把源代码文件全部放到一个目录下

2:该目录中编写一个文件用于通知Nginx如何编译本模块,文件名必须为config

3:在configure脚本执行时加入参数—add-module=PATH(PATH为存放源代码和config的目录)

当然也可以在执行完configure脚本后,修改生成的objs/Makefile和objs/ngx_modules.c文件。

 

 

config需要定义三个变量:(对于一个HTTP模块)

ngx_addon_name=ngx_http_mytest_module

HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

其中 $ngx_addon_dir等价于执行configure脚本时—add-module=PATH的PATH参数

 

ngx_http_request_s中

ngx_buf_t *header_in指向Nginx收到的未经解析的HTTP头部。

ngx_http_headers_in_t headers_in 存储已经解析过的HTTP头部。

 

异步接收包体的方法:ngx_http_read_client_request_body/ngx_http_client_body_handler_pt,接收完毕后调用后面的自定义的回调方法。

 

发送响应:

HTTP响应主要包括:响应行、响应头部、包体三部分。

发送HTTP响应需要执行发送HTTP头部(也会发送响应行)和发送HTTP包体两部操作。

发送HTTP头部:ngx_http_send_header方法

发送HTTP响应包体:ngx_http_output_filter方法

 

Nginx是全异步的服务器,不可以在进程的栈里分配内存并将其作为包体发送,应尽量从内存池里分配。

ngx_http_mytest_module.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);

static ngx_command_t ngx_http_mytest_commands[] = {
    { ngx_string("mytest"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
      ngx_http_mytest,
      NGX_HTTP_LOC_CONF_OFFSET,
      0,
      NULL },

      ngx_null_command
};

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);
    // 会在content阶段被HTTP框架调用
    clcf->handler = ngx_http_mytest_handler;
    return NGX_CONF_OK;
}

static ngx_http_module_t ngx_http_mytest_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_module_t ngx_http_mytest_module = {
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,    /* module context */
    ngx_http_mytest_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
};

static ngx_int_t
ngx_http_mytest_handler(ngx_http_request_t *r)
{
    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    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\n");
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = response.len;
    r->headers_out.content_type = type;

    // 发送HTTP头部
    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);
}

./configure --add-module=PATH

make

sudo make install

可以在/usr/local/nginx/conf/nginx.conf中加入下面几行:

location /foo {
            mytest;
        }


启动nginx后,测试:

curl localhost:80/foo

即会看到 Hello World!返回

 

 

用C++语言也编写HTTP模块

用C编译器来编译Nginx官方提供的各模块,用C++编译器来编译用C++语言开发的模块,最后利用C++向前兼容C语言的特性,使用C++编译器把所有的目标文件链接起来。才能正确的生成二进制文件Nginx。

保证C++编译的Nginx模块与C编译的Nginx模块互相适应,即能够互相调用,用C++提供的extern “C”特性实现。

 

修改configure生成的Makefile文件:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值