最近在讀陶輝的《深入理解Nginx:模塊開發與架構解析》,一是想跟着大牛練練閱讀和編寫開源代碼的能力,二是想學學Nginx優秀的架構設計,三是想找一個點深入下Linux下網絡編程的細節。侯捷在他的《STL源碼剖析》的自序裏說過,“追蹤一流作品並於其中吸取養分,遠比自己關起門來寫個三流作品,價值高得多”。我個人比較喜歡《深入理解Nginx》這本書,它講解的很全面,相關的知識都會有所介紹,整體的脈絡比較明晰。只是涉及東西比較多,有時真希望紙質文檔也能像在線lxr那樣給結構體和函數的使用處和定義做一個超鏈接,這樣來回跳轉查閱不會太麻煩。紙上得來終覺淺,絕知此事要躬行,讀書的時候自然少不了實際地動手。這個系列的博文便是對學習過程的梳理和動手過程的記錄。不過雖希望成“系列”,限於時間和精力,以及對nginx剛剛上手,我很難保證下一篇什麼時候能寫好,還請諒解。
另外,原書對實際動手時遇到的一些細節問題已有提及,我便不再贅述。如無特殊提示和說明,代碼均爲原書上的代碼。
一、安裝與配置
首先需要安裝Nginx要用到或者可能用到的軟件:GCC、pcre、zlib、OpenSSL。由於原書使用yum直接安裝,我又懶得在舊版(10.04)的Ubuntu下折騰安裝包,因此直接搞了個Fedora19虛擬機也就能直接用yum了。雖然看上去也挺麻煩,其實也不過是下下ISO鏡像、配配vim、搞個文件共享而已,輕車熟路。另外要注意的是:
我的Fedora19不帶fastest-mirror,最好先裝下這個yum的插件,不然後面的下載可能會慢死人不償命;
如果系統不帶C庫手冊,爲了便於編碼時查閱C庫函數,請yum install man-pages。
配置過程包括Linux內核參數優化(/etc/sysctl.conf)、Nginx源碼配置(可以直接下載、解壓、configure、make、make install)、nginx.conf的配置,照着原書上的來就是,nginx.conf的具體實踐留給下文。不過你可以藉機照着原書學學configure文件的寫法,這可是Linux下的一項通用技能。
另外爲了方便使用,我建立了個軟鏈接:先刪除/usr/bin/nginx,然後ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx。
二、HelloWorld的實現
Nginx封裝了一些數據結構,瞭解下即可,馬上就要用到。跟隨第三章的思路,要知道如何將自己寫的模塊編譯進Nginx:最簡單且不易出錯的是編寫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"
然後用C語言編寫模塊源碼。書中介紹了幾個數據結構和函數,經過梳理,它們的關係是這樣的:
它們完成任務的具體過程和編碼實現原書上都有,整合到源碼的對應目錄下(和config一致,這裏是nginx-1.4.2/my_module/helloword/)的ngx_http_mytest_module.c文件中即可。記得添加頭文件ngx_config.h、ngx_core.h和ngx_http.h:
#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 char* ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
clcf->handler = ngx_http_mytest_handler;
return NGX_CONF_OK;
}
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
};
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
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_str_t type = ngx_string("text/plain");
ngx_str_t response = ngx_string("Hello World!");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type;
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;
}
ngx_memcpy(b->pos,response.data,response.len);
b->last = b->pos + response.len;
b->last_buf = 1;
ngx_chain_t out;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r,&out);
}
最後一步是修改nginx.conf配置,爲http塊中增加一個對應於mytest模塊的location塊:
location /test {
mytest;
}
這裏有幾點需要注意的,我在這裏折騰了不少時間:
1.原書中寫的是Location,其實應該是location;
2.如果之前make install過,那麼如果修改的是源碼的nginx.conf,那麼只能複製到/usr/local/nginx/conf/nginx.conf.default,而不是nginx.conf,這時啓動nginx是不能生效的。這一點可以從Makefile文件中推斷出來,解決辦法是make install後將nginx.conf.default覆蓋nginx.conf,或者直接修改/usr/local/nginx/conf/nginx.conf。
此時啓動nginx,在終端輸入
curl http://localhost/test
就可以看到Hello World!了。