nginx 開發一個簡單的 HTTP 模塊
1. 下載 Nginx
http://nginx.org/
2. 目錄結構
$ tree -L 2 . ├── mytest_module │ ├── config │ └── ngx_http_mytest_module.c └── nginx ...
3. config
# 在 configure 執行時使用 ngx_addon_name="ngx_http_mytest_module"HTTP 模塊名稱
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
新增模塊源代碼
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"</pre>
4. ngx_http_mytest_module.c
/ mytest_module */include <ngx_config.h>
include <ngx_core.h>
include <ngx_http.h>
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t r); static char ngx_http_mytest(ngx_conf_t cf, ngx_command_t cmd, void *conf);
/ --ngx_command_t:用于處理 nginx.conf 中的配置項結構 */ 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 };
/ --ngx_http_module_t 上下文結構體 */ static ngx_http_module_t ngx_http_mytest_module_ctx = { NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL };
/ --ngx_module_t configure 后,會被加入到 ngx_modules.c 中 / ngx_module_t ngx_http_mytest_module = { // ctx_index,index,spare0,spare1,spare2,spare3,version NGX_MODULE_V1, // 上下文結構體 &ngx_http_mytest_module_ctx, // 用于處理 nginx.conf 中的配置項結構 ngx_http_mytest_commands, // 模塊類型,HTTP模塊 NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING };
// 處理 nginx.conf 配置項參數的方法 static char ngx_http_mytest(ngx_conf_t cf, ngx_command_t cmd, void conf) { ngx_http_core_loc_conf_t *clcf;
// 獲取 mytest 配置項所屬的配置塊 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
// 如果請求的主機域名、URI與mytest配置項所在的配置塊相匹配,調用該方法 clcf->handler = ngx_http_mytest_handler;
return NGX_CONF_OK; }
// 如果請求的主機域名、URI與mytest配置項所在的配置塊相匹配,調用該方法 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r) { // 必須是 GET 獲得 HEAD 方法 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 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 *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); b->last = b->pos + response.len; b->last_buf = 1; // 構造發送時的 ngx_chain_t 結構體 ngx_chain_t out; out.buf = b; out.next = NULL;
// 發送包體 return ngx_http_output_filter(r, &out); }</pre>
5. 編譯、運行
修改配置:
nginx$ vi conf/nginx.conf加入如下:
# (添加)非守護進程運行 daemon off; # (添加)master 自身處理請求 master_process on; events { ..... } http { .... server { # 修改 80 為 8080 listen 8080; .... # (添加) 到遇到 GET /test 請求時,使用 mytest 模塊處理 location /test { mytest; } ... } ... }configure:
nginx$ ./configure --prefix=/tmp/ngx_study/ --add-module=../mytest_module在運行 configure 后,會在 objs/ngx_modules.c 文件的 ngx_modules 數組中加入 ngx_http_mytest_module 信息:
$ cat objs/ngx_modules.c | grep http_mytest_module extern ngx_module_t ngx_http_mytest_module; &ngx_http_mytest_module,編譯:
nginx$ make nginx$ make install運行:
$ /tmp/ngx_study/sbin/nginx在瀏覽器中輸入:
localhost:8080/test6. PS 猜想:
nginx 通過 ngx_modules.c 中的結構 ngx_module_t ngx_modules ->
ngx_http_mytest_module.c 中的結構 ngx_module_t ngx_http_mytest_module -> 結構 ngx_command_t ngx_http_mytest_commands -> 函數ngx_http_mytest 設置 處理函數 ngx_http_mytest_handler。
參考:
《深入理解 Nginx ——模塊開發與架構解析》