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文件:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章