1. 如何調用HTTP子產品
本質:worker 程序會在一個for 循環裡面反複調用事件子產品檢測網絡事件。

2. 基本資料結構
- 命名規則: ngx_http_xxx_module.c
2.1 整形封裝
- ngx_int_t 封裝了有符号整形, 而 ngx_uint_t 封裝了無符号整形
2.2 ngx_str_t
- ngx_str_t 封裝了字元串
typedef struct { size_t len; u_char * data; }ngx_str_t;
- 可以使用 ngx_strncmp 進行名稱比對
2.3 ngx_list_t
- 這是一個連結清單容器, 容器内元素為數組
typedef struct ngx_list_part_s ngx_list_part_t; struct ngx_list_part_s{ void * elts; // 指向數組的起始位置 ngx_unit_t nelts; // 标記數組中已經使用的容量 ngx_list_part_t * next; }; typedef struct { ngx_list_part_t * last; // 指向連結清單的最後一個數組的位置 ngx_list_part_t part; // 指向連結清單頭 size_t size; // 一個連結清單元素(ie, 數組節點)所包含的元素大小 ngx_uint_t nalloc; // 一個連結清單元素(ie, 數組節點)所包含的元素個數 ngx_pool_t * pool; // 記憶體池位址 }ngx_list_t;
- ngx_list_t的記憶體分布圖
深入了解nginx chap3 開發一個簡單的HTTP子產品1. 如何調用HTTP子產品2. 基本資料結構3. 如何将自己的HTTP子產品編譯進Nginx4. HTTP子產品的資料結構5. 定義自己的HTTP 子產品6. 處理使用者請求7. 發送響應8. 發送磁盤檔案9. 用C++ 語言編寫HTTP 子產品10. 小結 -
連結清單的周遊
可以根據ngx_list_t 的結構類型, (外層連結清單, 内層數組), 實作結構的周遊操作
2.4 ngx_table_elt_t
本質是一個鍵值對結構
2.5 ngx_buf_t
緩沖區資料結構ngx_buf_t, 是處理大資料的關鍵資料結構, 既應用于記憶體資料也應用于磁盤資料
2.6 ngx_chain_t
一般與ngx_chain_t 配合使用, 用來作為發送包體的資料結構
3. 如何将自己的HTTP子產品編譯進Nginx
3.1 config
在configure執行的時候, 使用 –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"
編譯生成即可
3.2 configure 腳本
略
3.3 直接修改 makefile
略
4. HTTP子產品的資料結構
4.1 ngx_module_t 資料結構
首先, 需要了解的是 ngx_module_t 這個nginx 的子產品資料結構, 而在這個資料結構中, 我們最需要關注的是下面 3 個 變量:
- ctx, 用于指向一類子產品的上下文結構體 (對于HTTP 子產品, ctx 指針必須指向 ngx_http_module_t 接口)
- commands, 用于處理nginx.conf 檔案中的配置項
- type, 用來表示子產品的類型 (定義HTTP 子產品, 這個值應該設定為: NGX_HTTP_MODULE)
4.2 ngx_http_module_t 接口
HTTP 架構在啟動的過程中會在每個階段中調用ngx_http_module_t 中相應的方法
4.3 commands 數組
- 用來定義子產品的配置檔案參數, 數組中的每個元素都是 ngx_command_t 類型, 數組的結尾使用 ngx_null_command 來标記
- Nginx 解析配置檔案的流程:
- 解析每個配置項的時候, 首先會周遊所有的子產品
- 對于每個子產品而言, 通過周遊commands 數組進行, 查找他所感興趣的配置項
- 每個ngx_command_t 結構體定義了自己感興趣的一個配置項
5. 定義自己的HTTP 子產品
我們之前寫過一篇定義自己的hello world 子產品的博文
http://blog.csdn.net/zhyh1435589631/article/details/51638561
裡面有寫過這部分内容的配置
6. 處理使用者請求
- 在出現mytest 配置項的時候, ngx_http_mytest 方法被調用, 這時候, 将ngx_http_core_loc_conf_t 結構的handler 成員指定為ngx_http_mytest_handler。 在HTTP 架構接收完HTTP請求的頭部之後, 會調用handler指向的方法, 用來處理使用者請求。
6.1 處理方法的傳回值
- 主要包含RFC 2616 規範中定義的傳回碼, 以及Nginx 自身定義的HTTP傳回碼
- 主要需要了解4個通用的傳回碼:
- NGX_OK: 表示成功
- NGX_DECLINED: 繼續在NGX_HTTP_CONTENT_PHASE 階段, 尋找感興趣的HTTP 子產品來再次處理這個請求
- NGX_DONE: 表示到此為止, 同時HTTP 架構将暫時不再繼續執行這個請求的後續部分。事實上, 如果是keepalive類型的使用者請求, 就會保持這個http 連接配接, 然後把控制權傳回給Nginx
- NGX_ERROR: 表示錯誤, 将終止請求
6.2 擷取URI 和 參數
- 請求的所有資訊(方法, URI, 協定版本号和頭部等)都可以在傳入的ngx_http_request_t 類型的參數 r 中擷取。
- 可以通過r->method 這個整形成員與預定義的幾個宏進行比較, 進而擷取方法名
- URI, uri 成員指向使用者請求的 URI
- args, 指向使用者請求中的URI參數
- http_protocol 的data 成員指向使用者請求中HTTP 協定版本字元串的起始位址, len 成員為其字元串長度
6.3 擷取HTTP 頭部
ngx_http_headers_in_t 類型的headers_in存儲已經解析過的HTTP 頭部
6.4 擷取HTTP 包體
- 由于HTTP 包體的長度有可能非常大, 如果試圖一次性調用并讀完所有的包體, 可能導緻nginx 的阻塞
- nginx 提供一個叫做 ngx_http_read_client_request_body的異步方法, 調用它隻是說明要求Nginx開始連接配接請求的包體, 并不表示是否已經接收完畢。
- 如果 ngx_http_read_client_request_body是在ngx_http_mytest_handler處理方法中調用的話, 一般後者需要傳回 NGX_DONE
- 而如果不想處理請求中的包體的時候, 可以調用 ngx_http_discard_request_body 方法将接收自用戶端的HTTP 包體丢棄掉
7. 發送響應
- HTTP響應主要包括 3 個部分: 響應行, 響應頭部, 包體
7.1 發送HTTP頭部
- 使用api: ngx_http_send_header 即可
- 隻需要在發送之前, 預先指定一下 headers_out 中的成員即可
7.2 将記憶體中的字元串作為包體發送
- 調用 ngx_http_output_filter 可以實作向用戶端發送HTTP 響應包體
- 發送的内容放在 ngx_buf_t 結構中, 并使用 ngx_chain_t 傳遞給 ngx_http_output_filter 方法
7.3 hello world 示範
http://blog.csdn.net/zhyh1435589631/article/details/51638561
8. 發送磁盤檔案
8.1 如何發送磁盤中的檔案
- 發送檔案的接口和上面發送記憶體中資料的接口類似, 不同的是如何設定 緩沖區 ngx_buf_t
- 需要設定 ngx_buf_t 中的 in_file 标志
- 設定ngx_file_t 資料結構
深入了解nginx chap3 開發一個簡單的HTTP子產品1. 如何調用HTTP子產品2. 基本資料結構3. 如何将自己的HTTP子產品編譯進Nginx4. HTTP子產品的資料結構5. 定義自己的HTTP 子產品6. 處理使用者請求7. 發送響應8. 發送磁盤檔案9. 用C++ 語言編寫HTTP 子產品10. 小結 - 設定ngx_buf_t 中的 file_pos 和 file_last
8.2 清理檔案句柄
- nginx 通過異步方式将整個檔案高效的發送給使用者, 但是需要在發送完畢之後, 關閉打開的檔案句柄, 避免句柄洩露
- ngx_pool_cleanup_file 用于關閉檔案句柄
- ngx_pool_cleanup_t 結構
深入了解nginx chap3 開發一個簡單的HTTP子產品1. 如何調用HTTP子產品2. 基本資料結構3. 如何将自己的HTTP子產品編譯進Nginx4. HTTP子產品的資料結構5. 定義自己的HTTP 子產品6. 處理使用者請求7. 發送響應8. 發送磁盤檔案9. 用C++ 語言編寫HTTP 子產品10. 小結 - ngx_pool_cleanup_file_t 結構
深入了解nginx chap3 開發一個簡單的HTTP子產品1. 如何調用HTTP子產品2. 基本資料結構3. 如何将自己的HTTP子產品編譯進Nginx4. HTTP子產品的資料結構5. 定義自己的HTTP 子產品6. 處理使用者請求7. 發送響應8. 發送磁盤檔案9. 用C++ 語言編寫HTTP 子產品10. 小結 - 使用ngx_pool_cleanup_add 添加, 并設定完上面兩個結構即可
8.3 支援使用者多線程下載下傳和斷點續傳
r->allow_ranges = 1;
8.4 檔案傳輸效果
8.5 檔案傳輸的整體代碼
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
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,
,
NULL
},
ngx_null_command
};
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 */
};
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
};
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;
}
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;
}
ngx_buf_t *b;
b = ngx_palloc(r->pool, sizeof(ngx_buf_t));
//要打開的檔案
u_char* filename = (u_char*)"/tmp/test.txt";
b->in_file = ;
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
b->file->fd = ngx_open_file(filename, NGX_FILE_RDONLY | NGX_FILE_NONBLOCK, NGX_FILE_OPEN, );
b->file->log = r->connection->log;
b->file->name.data = filename;
b->file->name.len = sizeof(filename) - ;
if (b->file->fd <= )
{
return NGX_HTTP_NOT_FOUND;
}
//支援斷點續傳
r->allow_ranges = ;
//擷取檔案長度
if (ngx_file_info(filename, &b->file->info) == NGX_FILE_ERROR)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
//設定緩沖區指向的檔案塊
b->file_pos = ;
b->file_last = b->file->info.st_size;
ngx_pool_cleanup_t* cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
if (cln == NULL)
{
return NGX_ERROR;
}
cln->handler = ngx_pool_cleanup_file;
ngx_pool_cleanup_file_t *clnf = cln->data;
clnf->fd = b->file->fd;
clnf->name = b->file->name.data;
clnf->log = r->pool->log;
//設定傳回的Content-Type。注意,ngx_str_t有一個很友善的初始化宏
//ngx_string,它可以把ngx_str_t的data和len成員都設定好
ngx_str_t type = ngx_string("text/plain");
//設定傳回狀态碼
r->headers_out.status = NGX_HTTP_OK;
//響應包是有包體内容的,是以需要設定Content-Length長度
r->headers_out.content_length_n = b->file->info.st_size;
//設定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_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);
}
9. 用C++ 語言編寫HTTP 子產品
- 使用G++ 編譯C++ 代碼, 用 GCC 編譯nginx 代碼, 使用 C++ 編譯器連結
9.1 編譯方式修改
- 直接修改makefile
- LINK = $(CXX) 使用 g++ 連結
- C++ 檔案使用 $(CXX) 編譯
9.2 程式中的符号轉換
extern “C”{
}
10. 小結
ps: 處理方法必須是快速, 無阻塞的, ngx_http_mytest_handler 或者類似的處理方法中是不可以有耗時很長的操作的