nginx开发简单的http模块
轉(zhuǎn)載地址:https://blog.csdn.net/zhangxiao93/article/details/52836128
武俠世界的高手一般都是從掃地僧做起的,程序員學(xué)一門技術(shù)都是從hello world開始。
本文介紹開發(fā)一個簡單的hello world模塊。
(一) 如何將自己的http模塊編譯進Nginx
首先創(chuàng)建源碼目錄:
mkdir /home/zhangxiao/nginx/nginx-1.0.15/src/test/
在編譯Nginx之前,執(zhí)行configure腳本時,添加--add-module=PATH例如:
./configure --add-module=/home/zhangxiao/nginx/nginx-1.0.15/src/test
1.編寫config文件
config文件在上述目錄中,這個文件將通知如何編譯本模塊。
config文件其實是一個可執(zhí)行的Shell腳本,如果只想開發(fā)一個HTTP模塊,需要定義三個變量:
? (1) ngx_addon_name
? ? ? 僅在configure執(zhí)行時使用,一般設(shè)置為模塊名稱。
? (2) HTTP_MODULES
? ? ? 保存所有的HTTP模塊名稱。每個模塊間由空格相連。在重新設(shè)置這個變量時,不要直接覆蓋,因此要如下設(shè)置:
"$HTTP_MODULES ngx_http_mytest_module"
? ?(3) NGX_ADDON_SRCS
? ? ?用于指定新模塊的源代碼,多個待編譯的源代碼之間可以用空格相連。
? ? ?注意,在設(shè)置這個變量時可以使用$ngx_addon_dir變量,它等價于configure執(zhí)行時--add-module=PATH的PATH參數(shù)
? ? ?因此,我們的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"
2.修改nginx.conf配置文件
sudo vi /usr/local/nginx/conf/nginx.conf
在server配置塊下添加location配置塊,并在新添加的location配置塊中添加mytest配置項:
#nginx.conf刪去一部分內(nèi)容http {server {listen 80;server_name localhost;#########添加部分######### location=/zxtest{ #mytest配置項mytest; } #########################location / {proxy_pass http://localhost:8080;} }3.定義http模塊處理用戶請求
#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, //類型typengx_http_mytest, //set 函數(shù)NGX_HTTP_LOC_CONF_OFFSET, //偏移0,NULL},ngx_null_command };//回調(diào)函數(shù),出現(xiàn)my_test配置項后調(diào)用 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所在配置塊匹配時,將調(diào)用handlerclcf->handler = ngx_http_mytest_handler;return NGX_CONF_OK; } //ngx_http_module_t接口,模塊上下文 //如果沒有什么工作是必須在HTTP框架初始化時完成,則不必實現(xiàn)8個回調(diào) 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) {//請求方法必須是GET或者HEAD,否則返回405/NOT ALLOWEDif (!(r->method &(NGX_HTTP_GET|NGX_HTTP_HEAD))){return NGX_HTTP_NOT_ALLOWED;}//丟棄請求中的包體ngx_int_t rc = ngx_http_discard_request_body(r);if (NGX_OK != rc){return rc;}//設(shè)置返回的Content-Type ngx_string是一個宏可以初始化data字段和len字段ngx_str_t type = ngx_string("text/plain");ngx_str_t response = ngx_string("Hello ZhangXiao World");//響應(yīng)包體內(nèi)容和狀態(tài)碼設(shè)置r->headers_out.status = NGX_HTTP_OK;r->headers_out.content_length_n = response.len;r->headers_out.content_type = type;//發(fā)送http頭部rc = ngx_http_send_header(r);if (rc == NGX_ERROR || rc>NGX_OK || r->header_only) {return rc;}//構(gòu)造ngx_buf_t結(jié)構(gòu)體準備發(fā)送報文ngx_buf_t *b;b = ngx_create_temp_buf(r->pool, response.len);if (NULL == b) {return NGX_HTTP_INTERNAL_SERVER_ERROR;}//拷貝響應(yīng)報文ngx_memcpy(b->pos, response.data, response.len);b->last = b->pos+response.len;//聲明這是最后一塊緩沖區(qū)b->last_buf = 1;//構(gòu)造發(fā)送時的ngx_chain_t結(jié)構(gòu)體ngx_chain_t out;out.buf = b;out.next = NULL;//發(fā)送響應(yīng),結(jié)束后HTTP框架會調(diào)用ngx_http_finalize_request方法結(jié)束請求return ngx_http_output_filter(r, &out); }4.編譯測試
./configure --add-module=/home/zhangxiao/nginx/nginx-1.0.15/src/test
make
sudo make install
重啟nginx
./usr/local/nginx/sbin
在瀏覽器輸入localhost/zxtest進行測試驗證?
(二)參考
1.深入理解Nginx?
2.http://blog.csdn.net/xiajun07061225/article/details/9130237?
3.http://kb.cnblogs.com/page/98352/
總結(jié)
以上是生活随笔為你收集整理的nginx开发简单的http模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 带超时的system
- 下一篇: curl的使用