編寫HTTP模塊
- 幾個重要組成部分
ngx_command_t 數(shù)組
對于我們在nginx.conf 中編寫的配置項 mytest 來說琳骡, nginx 首先會遍歷所有的模塊(modules)委煤,而對于每個模塊, 會遍歷他所對應(yīng)的ngx_command_t 數(shù)組, 試圖找到關(guān)于我們的配置項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中用于處理配置項參數(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配置項所屬的配置塊虎敦,clcf貌似是location塊內(nèi)的數(shù)據(jù)
//結(jié)構(gòu)挺狰,其實不然,它可以是main廷区、srv或者loc級別配置項唯灵,也就是說在每個
//http{}和server{}內(nèi)也都有一個ngx_http_core_loc_conf_t結(jié)構(gòu)體
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
//http框架在處理用戶請求進(jìn)行到NGX_HTTP_CONTENT_PHASE階段時,如果
//請求的主機域名隙轻、URI與mytest配置項所在的配置塊相匹配埠帕,就將調(diào)用我們
//實現(xiàn)的ngx_http_mytest_handler方法處理這個請求
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, 匹配項被選中的時候, 應(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è)置三個內(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 模塊在編譯的時候狡蝶, 就可以被加入到ngx_modules的全局?jǐn)?shù)組中了
處理用戶請求的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;
}
//設(shè)置返回的Content-Type苍姜。注意牢酵,ngx_str_t有一個很方便的初始化宏
//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長度
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ā)送時的ngx_chain_t結(jié)構(gòu)體
ngx_chain_t out;
//賦值ngx_buf_t
out.buf = b;
//設(shè)置next為NULL
out.next = NULL;
//最后一步發(fā)送包體,http框架會調(diào)用ngx_http_finalize_request方法
//結(jié)束請求
return ngx_http_output_filter(r, &out);
}
將HTTP模塊編譯到nginx中
模塊的源代碼應(yīng)該和config文件放到一個目錄下面垫释,然后在編譯的時候加入?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;
}
運行效果如下: