实现第一个自定义nginx模块
生活随笔
收集整理的這篇文章主要介紹了
实现第一个自定义nginx模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現第一個自定義nginx模塊
下面的過程詳細記錄了如何實現第一個自定義的nginx模塊,對nginx入門者包括我很有參考價值,特記錄如下。
前提
假定以root身份已經在CentOS 6.8 x86上
創建第三方子模塊所在目錄
mkdir -p /usr/local/src/nginx_modules_demo/ngx_http_mytest_module
在里面放入如下文件,內容參見文章末尾
ngx_http_mytest_module.c
config
關閉nginx
/usr/local/nginx/sbin/nginx -s quit
ps auxf | grep nginx
源碼編譯生成第三方模塊
./configure --with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.0e \
--add-module=/usr/local/src/nginx_modules_demo/ngx_http_mytest_module
注意之后直接配置第三方模塊,原有已經變化
?
make
make install
修改nginx配置文件,添加指定location塊
vim /usr/local/nginx/config/nginx.config
在nginx.config中添加
location /test{ ?
? ?mytest;
}
開啟nginx
/usr/local/nginx/sbin/nginx
從瀏覽器中訪問
地址欄中輸入
http://localhost/test
下面的過程詳細記錄了如何實現第一個自定義的nginx模塊,對nginx入門者包括我很有參考價值,特記錄如下。
前提
假定以root身份已經在CentOS 6.8 x86上
創建第三方子模塊所在目錄
mkdir -p /usr/local/src/nginx_modules_demo/ngx_http_mytest_module
在里面放入如下文件,內容參見文章末尾
ngx_http_mytest_module.c
config
關閉nginx
/usr/local/nginx/sbin/nginx -s quit
ps auxf | grep nginx
源碼編譯生成第三方模塊
./configure --with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.0e \
--add-module=/usr/local/src/nginx_modules_demo/ngx_http_mytest_module
注意之后直接配置第三方模塊,原有已經變化
?
make
make install
修改nginx配置文件,添加指定location塊
vim /usr/local/nginx/config/nginx.config
在nginx.config中添加
location /test{ ?
? ?mytest;
}
開啟nginx
/usr/local/nginx/sbin/nginx
從瀏覽器中訪問
地址欄中輸入
http://localhost/test
如何看到如下結果,說明該第三方模塊ngx_http_mytest_module
ngx_http_mytest_module.c文件內容
#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); //定義模塊配置文件的處理
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 }, //command數組要以ngx_null_command結束 //#define ngx_null_command {ngx_null_string,0,NULL,0,0,NULL} ngx_null_command }; //mytest模塊上下文,都為NULL即是說在http框架初始化時沒有什么要做 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模塊的定義,在編譯時加入到全局的ngx_modules數組中,這樣在Nginx初始化時會調用模塊的所有初始化方法,(上面的ngx_http_module_t類型的ngx_http_mytest_module_ctx) ngx_module_t ngx_http_mytest_module = { NGX_MODULE_V1, //由Nginx定義的宏來初始化前七個成員 &ngx_http_mytest_module_ctx, //模塊的上下文結構體,指向特定模塊的公共方法 ngx_http_mytest_commands, //處理配置項的結構體數組 NGX_HTTP_MODULE, //模塊類型 //Nginx在啟動停止過程中七個執行點的函數指針 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING //由Nginx定義的宏定義剩下的8個保留字段 }; //配置項對應的回調函數,當配置項中出現mytest配置項時將調用這個函數 static char * ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { //ckcf并不是指特定的location塊內的數據結構,他可以是mian、srv、loc級別的配置項 //每個http{},sever{},location{}都有一個ngx_http_core_loc_conf_t類型的數據結構 ngx_http_core_loc_conf_t *clcf; //找到mytest配置項所在的配置塊 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); //http框架在處理用戶請求進行到NGX_HTTP_CONTENT_PHASE階段是,如果請求的主機名,URI與配置項所在的配置塊相匹配時,就調用 //clcf中的handle方法處理這個請求 //NGX_HTTP_CONTENT_PHASE用于處理http請求內容的階段,這是大部分http模塊通常介入的階段 clcf->handler = ngx_http_mytest_handler; return NGX_CONF_OK; } //實際完成處理的回調函數 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 ! \n\rthis is my first Nginx module test ! "); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = response.len; 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; //根據請求中傳來的內存池對象,創建內存buf b = ngx_create_temp_buf(r->pool, response.len); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } //有效內容從pos位置開始,復制respon的內容 ngx_memcpy(b->pos, response.data, response.len); //有效內容到last結束 b->last = b->pos + response.len; //因為ngx_buf_t可以由ngx_chain_t鏈表鏈起來,last_buf可以標記這是最后一塊待處理的緩沖區,簡化處理 b->last_buf = 1; //將內存buf用鏈表鏈起來,作為ngx_http_output_filter的跌入個參數 ngx_chain_t out; out.buf = b; //標記這是最后一個ngx_chain_t out.next = NULL; return ngx_http_output_filter(r, &out); } config文件的內容
#僅在configure執行時使用,一般設置為模塊名稱
ngx_addon_name=ngx_http_mytest_module
#HTTP_MODULES保存所有的模塊名稱,在重設HTTP_MODULES時不能直接覆蓋,而是先取得以前的HTTP_MODULES,在加上自己的模塊
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
#指定新增源代碼文件
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
參考文獻
[1].http://blog.csdn.net/hustraiet/article/details/11519375
總結
以上是生活随笔為你收集整理的实现第一个自定义nginx模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在CentOS 6.8 x86_64上安
- 下一篇: There was a problem