Nginx編寫Hello World模塊

編寫HTTP模塊

  • 幾個重要組成部分
    ngx_command_t 數組

    對于我們在nginx.conf 中編寫的配置項 mytest 來說, nginx 首先會遍歷所有的模塊(modules),而對于每個模塊, 會遍歷他所對應的ngx_command_t 數組, 試圖找到關于我們的配置項mytest 的解析方式。
    這里編寫的hello模塊的代碼部分如下:
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
};

command中用于處理配置項參數的set 方法

static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_core_loc_conf_t  *clcf;

    //首先找到mytest配置項所屬的配置塊,clcf貌似是location塊內的數據
//結構,其實不然,它可以是main、srv或者loc級別配置項,也就是說在每個
//http{}和server{}內也都有一個ngx_http_core_loc_conf_t結構體
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

    //http框架在處理用戶請求進行到NGX_HTTP_CONTENT_PHASE階段時,如果
//請求的主機域名、URI與mytest配置項所在的配置塊相匹配,就將調用我們
//實現的ngx_http_mytest_handler方法處理這個請求
    clcf->handler = ngx_http_mytest_handler;

    return NGX_CONF_OK;
}

關于ngx_http_conf_get_module_loc_conf 的定義可以參考: http://lxr.nginx.org/source/src/http/ngx_http_config.h#0065 本質: 就是設置ngx_http_mytest_handler, 匹配項被選中的時候, 應該如何解析。
** 定義ngx_http_module_t 接口**
這部分的代碼, 是用于http框架的, 相當于http框架的回掉函數, 由于這里并不需要框架做任何操作,所以全部設置成NULL即可。

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 */
};

定義mytest模塊
mytest模塊的詳細內容解析

這里的模塊只需要設置三個內容:

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
};

設置完成ngx_module_t數組后,mytest 模塊在編譯的時候, 就可以被加入到ngx_modules的全局數組中了

處理用戶請求的hello world handler
該方法是配置項匹配之后的處理方法:

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    //必須是GET或者HEAD方法,否則返回405 Not Allowed
    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;
    }

    //設置返回的Content-Type。注意,ngx_str_t有一個很方便的初始化宏
//ngx_string,它可以把ngx_str_t的data和len成員都設置好
    ngx_str_t type = ngx_string("text/plain");
    //返回的包體內容
    ngx_str_t response = ngx_string("Hello World!");
    //設置返回狀態碼
    r->headers_out.status = NGX_HTTP_OK;
    //響應包是有包體內容的,所以需要設置Content-Length長度
    r->headers_out.content_length_n = response.len;
    //設置Content-Type
    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結構準備發送包體
    ngx_buf_t                 *b;
    b = ngx_create_temp_buf(r->pool, response.len);
    if (b == NULL)
    {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    //將Hello World拷貝到ngx_buf_t指向的內存中
    ngx_memcpy(b->pos, response.data, response.len);
    //注意,一定要設置好last指針
    b->last = b->pos + response.len;
    //聲明這是最后一塊緩沖區
    b->last_buf = 1;

    //構造發送時的ngx_chain_t結構體
    ngx_chain_t     out;
    //賦值ngx_buf_t
    out.buf = b;
    //設置next為NULL
    out.next = NULL;

    //最后一步發送包體,http框架會調用ngx_http_finalize_request方法
//結束請求
    return ngx_http_output_filter(r, &out);
}

將HTTP模塊編譯到nginx中

模塊的源代碼應該和config文件放到一個目錄下面,然后在編譯的時候加入參數, –add-module=PATH。其中config文件的內容如下所示:

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"

配置nginx.conf

location \hello{
    mytest;
}

運行效果如下:


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Nginx的配置項 nginx 在每一個http 塊, server 塊, location 塊下, 都會生成獨立...
    Spike_3154閱讀 4,271評論 0 1
  • 框架代碼分析 核心模塊 啟動過程(main) 1、全局ngx_cycle_t對象 1、ngx_init_cycle...
    AKEEM閱讀 1,155評論 1 0
  • 本文系轉載》》》》》》》》》》》》》》》》 編者按:高可用架構分享及傳播在架構領域具有典型意義的文章,本文由陳科在...
    demop閱讀 8,128評論 0 7
  • 第一章 Nginx簡介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 32,803評論 24 1,002
  • HTTP模塊的調用 worker 進程會在一個for 循環里面反復調用事件模塊檢測網絡事件。 基本數據結構 對整形...
    Spike_3154閱讀 1,592評論 0 1