編寫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;
}
運行效果如下: