Nginx Module擴展模塊實現


功能:編譯nginx 擴展模塊,可開發nginx自定義指令、完善NGINX mirror 旁路方式。

./configure --add-module=/path/to/nginx_http_echo_module.c

/*
 * @file:    nginx_http_echo_module.c
 * @brief:   Nginx echo command output a string
 * @author:  Panda <itwujunze@163.com>
 * @version: 1.0.1
 * @date:    2017/6/12
 *
 * Compile:
 *           shell> ./configure --add-module=/path/to/nginx_http_echo_module.c
 *
 */

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

/**
 *  定義模塊配置結構   命名規則為ngx_http_[module-name]_[main|srv|loc]_conf_t。其中main、srv和loc分別用於表示同一模塊在三層block中的配置信息。
 */
typedef struct {
    ngx_str_t ed;  //該結構體定義在這里 https://github.com/nginx/nginx/blob/master/src/core/ngx_string.h
} ngx_http_echo_loc_conf_t;

static char *ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_echo_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_echo_init(ngx_conf_t *cf);

/**
 * 定義echo模塊的指令
 */
static ngx_command_t ngx_http_echo_commands[] = {
        {ngx_string("echo"),
                NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
                ngx_http_echo,
                NGX_HTTP_LOC_CONF_OFFSET,
                offsetof(ngx_http_echo_loc_conf_t, ed),
                NULL,
        },
        ngx_null_command,
};

/**
 * 定義ngx_http_module_t類型的結構體變量   命名規則為ngx_http_[module-name]_module_ctx,這個結構主要用於定義各個Hook函數
 *
 * 可以看到一共有8個Hook注入點,分別會在不同時刻被Nginx調用,由於我們的模塊僅僅用於location域,這里將不需要的注入點設為NULL即可。
 *
 * ngx_http_echo_create_loc_conf  ngx_http_echo_merge_loc_conf 這兩個函數會被Nginx自動調用。注意這里的命名規則:ngx_http_[module-name]_[create|merge]_[main|srv|loc]_conf。
 */
static ngx_http_module_t ngx_http_echo_module_ctx = {
        NULL,                                  /* preconfiguration */
        ngx_http_echo_init,                    /* postconfiguration */
        NULL,                                  /* create main configuration */
        NULL,                                  /* init main configuration */
        NULL,                                  /* create server configuration */
        NULL,                                  /* merge server configuration */
        ngx_http_echo_create_loc_conf,         /* create location configration */
        ngx_http_echo_merge_loc_conf           /* merge location configration */
};

/**
 * 組合Nginx Module
 *
 * 上面完成了Nginx模塊各種組件的開發下面就是將這些組合起來了。一個Nginx模塊被定義為一個ngx_module_t結構體https://github.com/nginx/nginx/blob/master/src/core/ngx_module.h,這個結構體的字段很多,不過開頭和結尾若干字段一般可以通過Nginx內置的宏去填充
 *
 * 開頭和結尾分別用NGX_MODULE_V1和NGX_MODULE_V1_PADDING 填充了若干字段,就不去深究了。
 * 這里主要需要填入的信息從上到下以依次為context、指令數組、模塊類型以及若干特定事件的回調處理函數(不需要可以置為NULL),
 * 其中內容還是比較好理解的,注意我們的echo是一個HTTP模塊,所以這里類型是NGX_HTTP_MODULE,其它可用類型還有NGX_EVENT_MODULE(事件處理模塊)和NGX_MAIL_MODULE(郵件模塊)。
 *
 */
ngx_module_t ngx_http_echo_module = {
        NGX_MODULE_V1,
        &ngx_http_echo_module_ctx,             /* module context */
        ngx_http_echo_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,                                  /* exi master */
        NGX_MODULE_V1_PADDING
};

/**
 * Handler會接收一個ngx_http_request_t指針類型的參數,這個參數指向一個ngx_http_request_t結構體,此結構體存儲了這次HTTP請求的一些信息,這個結構定義在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h中:
 *
 * 第一步是獲取模塊配置信息,這一塊只要簡單使用ngx_http_get_module_loc_conf就可以了。
 *
 * 第二步是功能邏輯,因為echo模塊非常簡單,只是簡單輸出一個字符串,所以這里沒有功能邏輯代碼。
 *
 * 第三步是設置response header。Header內容可以通過填充headers_out實現,我們這里只設置了Content-type和Content-length等基本內容,ngx_http_headers_out_t定義了所有可以設置的HTTP Response Header信息 這個結構體在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h 設置好頭信息后使用ngx_http_send_header就可以將頭信息輸出,ngx_http_send_header接受一個ngx_http_request_t類型的參數。
 *
 * 第四步也是最重要的一步是輸出Response body。   Nginx的I/O機制  Nginx允許handler一次產生一組輸出,可以產生多次,Nginx將輸出組織成一個單鏈表結構,鏈表中的每個節點是一個chain_t,定義在https://github.com/nginx/nginx/blob/master/src/core/ngx_buf.h
 * @param r   ngx_http_request_t指針
 * @return
 */
static ngx_int_t
ngx_http_echo_handler(ngx_http_request_t *r)
{
    ngx_int_t rc;
    ngx_buf_t *b;
    ngx_chain_t out;
    ngx_http_echo_loc_conf_t *elcf;
    elcf = ngx_http_get_module_loc_conf(r,ngx_http_echo_module);
    if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
    {
        return NGX_HTTP_NOT_ALLOWED;
    }
    r->headers_out.content_type.len= sizeof("text/html") - 1;
    r->headers_out.content_type.data = (u_char *) "text/html";
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = elcf->ed.len;
    if(r->method == NGX_HTTP_HEAD)
    {
        rc = ngx_http_send_header(r);
        if(rc != NGX_OK)
        {
            return rc;
        }
    }
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if(b == NULL)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    out.buf = b;
    out.next = NULL;
    b->pos = elcf->ed.data;
    b->last = elcf->ed.data + (elcf->ed.len);
    b->memory = 1;
    b->last_buf = 1;
    rc = ngx_http_send_header(r);
    if(rc != NGX_OK)
    {
        return rc;
    }
    return ngx_http_output_filter(r, &out);
}

/**
 * 參數轉化函數
 * @param f
 * @param cmd
 * @param conf
 * @return ngx status code
 */
static char *
ngx_http_echo(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_echo_handler;  //修改核心模塊配置(也就是當前location),將其handler替換為我們自己定義的ngx_http_echo_handler
        ngx_conf_set_str_slot(cf,cmd,conf);
        return NGX_CONF_OK;
}



/**
 * 初始化一個配置結構體
 * @param cf
 * @return
 */
static void *
ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
{
        ngx_http_echo_loc_conf_t *conf;
        conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_loc_conf_t)); //gx_pcalloc用於在Nginx內存池中分配一塊空間,是pcalloc的一個包裝
        if(conf == NULL) {
                return NGX_CONF_ERROR;
        }
        conf->ed.len = 0;
        conf->ed.data = NULL;
        return conf;
}
/**
 * 將其父block的配置信息合並到此結構體 實現了配置的繼承
 * @param cf
 * @param parent
 * @param child
 * @return ngx status code
 *
 * ngx_conf_merge_str_value不是一個函數,而是一個宏,其定義在https://github.com/nginx/nginx/blob/master/src/core/ngx_conf_file.h#L205中
 */
static char *
ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_http_echo_loc_conf_t *prev = parent;
    ngx_http_echo_loc_conf_t *conf = child;
    ngx_conf_merge_str_value(conf->ed, prev->ed, '"');
    return NGX_CONF_OK;
}

/**
 * init echo模塊
 * @param cf
 * @return
 */
static ngx_int_t
ngx_http_echo_init(ngx_conf_t *cf)
{
    return NGX_OK;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM