Nginx編寫(xiě)Hello World模塊

編寫(xiě)HTTP模塊

  • 幾個(gè)重要組成部分
    ngx_command_t 數(shù)組

    對(duì)于我們?cè)趎ginx.conf 中編寫(xiě)的配置項(xiàng) mytest 來(lái)說(shuō), nginx 首先會(huì)遍歷所有的模塊(modules),而對(duì)于每個(gè)模塊, 會(huì)遍歷他所對(duì)應(yīng)的ngx_command_t 數(shù)組, 試圖找到關(guān)于我們的配置項(xiàng)mytest 的解析方式。
    這里編寫(xiě)的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中用于處理配置項(xiàng)參數(shù)的set 方法

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

    //首先找到mytest配置項(xiàng)所屬的配置塊,clcf貌似是location塊內(nèi)的數(shù)據(jù)
//結(jié)構(gòu),其實(shí)不然,它可以是main、srv或者loc級(jí)別配置項(xiàng),也就是說(shuō)在每個(gè)
//http{}和server{}內(nèi)也都有一個(gè)ngx_http_core_loc_conf_t結(jié)構(gòu)體
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

    //http框架在處理用戶請(qǐng)求進(jìn)行到NGX_HTTP_CONTENT_PHASE階段時(shí),如果
//請(qǐng)求的主機(jī)域名、URI與mytest配置項(xiàng)所在的配置塊相匹配,就將調(diào)用我們
//實(shí)現(xiàn)的ngx_http_mytest_handler方法處理這個(gè)請(qǐng)求
    clcf->handler = ngx_http_mytest_handler;

    return NGX_CONF_OK;
}

關(guān)于ngx_http_conf_get_module_loc_conf 的定義可以參考: http://lxr.nginx.org/source/src/http/ngx_http_config.h#0065 本質(zhì): 就是設(shè)置ngx_http_mytest_handler, 匹配項(xiàng)被選中的時(shí)候, 應(yīng)該如何解析。
** 定義ngx_http_module_t 接口**
這部分的代碼, 是用于http框架的, 相當(dāng)于http框架的回掉函數(shù), 由于這里并不需要框架做任何操作,所以全部設(shè)置成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模塊的詳細(xì)內(nèi)容解析

這里的模塊只需要設(shè)置三個(gè)內(nèi)容:

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

設(shè)置完成ngx_module_t數(shù)組后,mytest 模塊在編譯的時(shí)候, 就可以被加入到ngx_modules的全局?jǐn)?shù)組中了

處理用戶請(qǐng)求的hello world handler
該方法是配置項(xiàng)匹配之后的處理方法:

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

    //丟棄請(qǐng)求中的包體
    ngx_int_t rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK)
    {
        return rc;
    }

    //設(shè)置返回的Content-Type。注意,ngx_str_t有一個(gè)很方便的初始化宏
//ngx_string,它可以把ngx_str_t的data和len成員都設(shè)置好
    ngx_str_t type = ngx_string("text/plain");
    //返回的包體內(nèi)容
    ngx_str_t response = ngx_string("Hello World!");
    //設(shè)置返回狀態(tài)碼
    r->headers_out.status = NGX_HTTP_OK;
    //響應(yīng)包是有包體內(nèi)容的,所以需要設(shè)置Content-Length長(zhǎng)度
    r->headers_out.content_length_n = response.len;
    //設(shè)置Content-Type
    r->headers_out.content_type = type;

    //發(fā)送http頭部
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
    {
        return rc;
    }

    //構(gòu)造ngx_buf_t結(jié)構(gòu)準(zhǔn)備發(fā)送包體
    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指向的內(nèi)存中
    ngx_memcpy(b->pos, response.data, response.len);
    //注意,一定要設(shè)置好last指針
    b->last = b->pos + response.len;
    //聲明這是最后一塊緩沖區(qū)
    b->last_buf = 1;

    //構(gòu)造發(fā)送時(shí)的ngx_chain_t結(jié)構(gòu)體
    ngx_chain_t     out;
    //賦值ngx_buf_t
    out.buf = b;
    //設(shè)置next為NULL
    out.next = NULL;

    //最后一步發(fā)送包體,http框架會(huì)調(diào)用ngx_http_finalize_request方法
//結(jié)束請(qǐng)求
    return ngx_http_output_filter(r, &out);
}

將HTTP模塊編譯到nginx中

模塊的源代碼應(yīng)該和config文件放到一個(gè)目錄下面,然后在編譯的時(shí)候加入?yún)?shù), –add-module=PATH。其中config文件的內(nèi)容如下所示:

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

運(yùn)行效果如下:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Nginx的配置項(xiàng) nginx 在每一個(gè)http 塊, server 塊, location 塊下, 都會(huì)生成獨(dú)立...
    Spike_3154閱讀 4,467評(píng)論 0 1
  • 框架代碼分析 核心模塊 啟動(dòng)過(guò)程(main) 1、全局ngx_cycle_t對(duì)象 1、ngx_init_cycle...
    AKEEM閱讀 1,259評(píng)論 1 0
  • 本文系轉(zhuǎn)載》》》》》》》》》》》》》》》》 編者按:高可用架構(gòu)分享及傳播在架構(gòu)領(lǐng)域具有典型意義的文章,本文由陳科在...
    demop閱讀 8,362評(píng)論 0 7
  • 第一章 Nginx簡(jiǎn)介 Nginx是什么 沒(méi)有聽(tīng)過(guò)Nginx?那么一定聽(tīng)過(guò)它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,018評(píng)論 24 1,002
  • HTTP模塊的調(diào)用 worker 進(jìn)程會(huì)在一個(gè)for 循環(huán)里面反復(fù)調(diào)用事件模塊檢測(cè)網(wǎng)絡(luò)事件。 基本數(shù)據(jù)結(jié)構(gòu) 對(duì)整形...
    Spike_3154閱讀 1,640評(píng)論 0 1

友情鏈接更多精彩內(nèi)容