Nginx 定义自己的 http 模块

转自:http://blog.csdn.net/u012243115/article/details/46898573

一、config文件编写

        Nginx提供了一种简单的方式将第三方的模块编译到Nginx。首先把源代码文件全部放到一个目录下,同时在该目录中编写一个文件用于通知Nginx如何编译本模块,这个文件名必须为config。然后,在configure脚本执行时加入参数--add-module=PATH(新模块源代码以及config文件存放目录),就可以在编译Nginx之前把新模块添加进去,然后再编译(make)即可。

config文件格式

        config文件其实是一个可执行的Shell脚本,如果只想开发一个HTTP模块,需要定义三个变量:

(1)ngx_adon_name

仅在configure执行时使用,一般设置为模块名称。

(2)HTTP_MODULES

保存所有的HTTP模块名称。每个模块间由空格相连。在重新设置这个变量时,不要直接覆盖,因此要如下设置:

"$HTTP_MODULESngx_http_mytest_module"

(3)NGX_ADDON_SRCS

用于指定新模块的源代码,多个待编译的源代码之间可以用空格相连。

注意,在设置这个变量时可以使用$ngx_addon_dir变量,它等价于configure执行时--add-module=PATH的PATH参数。

本例中的config文件内容如下:

[cpp]  view plain  copy
  1. ngx_addon_name=ngx_http_mytest_module  
  2. HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"  
  3. NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"  

二、定义HTTP模块及处理用户请求

        用C语言编写模块源码ngx_http_mytest_module.c。书中介绍了几个数据结构和函数,经过梳理,它们的关系是这样的:


        它们完成任务的具体过程和编码实现原书上都有(一些数据传递如HTTP请求的解析、数据传递是由Nginx的HTTP框架完成的,在模块代码中并未体现),记得添加头文件ngx_config.h、ngx_core.h和ngx_http.h:

ngx_http_mytest_module.c文件(与上面的config文件位于同一目录下)如下:

源码来源:http://nginx.weebly.com/31034203632830430721.html

[cpp]  view plain  copy
  1. #include <ngx_config.h>  
  2. #include <ngx_core.h>  
  3. #include <ngx_http.h>  
  4.   
  5.   
  6. static char *  
  7. ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);  
  8.   
  9. static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);  
  10.   
  11.   
  12. //处理配置项    
  13. static ngx_command_t  ngx_http_mytest_commands[] =  
  14. {  
  15.   
  16.     {  
  17.         ngx_string("mytest"),  
  18.         NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,  
  19.         ngx_http_mytest,  
  20.         NGX_HTTP_LOC_CONF_OFFSET,  
  21.         0,  
  22.         NULL  
  23.     },  
  24.   
  25.     ngx_null_command  
  26. };  
  27. //模块上下文  
  28. static ngx_http_module_t  ngx_http_mytest_module_ctx =  
  29. {  
  30.     NULL,                              /* preconfiguration */  
  31.     NULL,                       /* postconfiguration */  
  32.   
  33.     NULL,                              /* create main configuration */  
  34.     NULL,                              /* init main configuration */  
  35.   
  36.     NULL,                              /* create server configuration */  
  37.     NULL,                              /* merge server configuration */  
  38.   
  39.     NULL,                   /* create location configuration */  
  40.     NULL                    /* merge location configuration */  
  41. };  
  42. //新模块定义    
  43. ngx_module_t  ngx_http_mytest_module =  
  44. {  
  45.     NGX_MODULE_V1,  
  46.     &ngx_http_mytest_module_ctx,           /* module context */  
  47.     ngx_http_mytest_commands,              /* module directives */  
  48.     NGX_HTTP_MODULE,                       /* module type */  
  49.     NULL,                                  /* init master */  
  50.     NULL,                                  /* init module */  
  51.     NULL,                                  /* init process */  
  52.     NULL,                                  /* init thread */  
  53.     NULL,                                  /* exit thread */  
  54.     NULL,                                  /* exit process */  
  55.     NULL,                                  /* exit master */  
  56.     NGX_MODULE_V1_PADDING  
  57. };  
  58.   
  59. //配置项对应的回调函数   
  60. static char *  
  61. ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  62. {  
  63.     ngx_http_core_loc_conf_t  *clcf;  
  64.   
  65.     //首先找到mytest配置项所属的配置块,clcf貌似是location块内的数据  
  66. //结构,其实不然,它可以是main、srv或者loc级别配置项,也就是说在每个  
  67. //http{}和server{}内也都有一个ngx_http_core_loc_conf_t结构体  
  68.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);  
  69.   
  70.     //http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时,如果  
  71. //请求的主机域名、URI与mytest配置项所在的配置块相匹配,就将调用我们  
  72. //实现的ngx_http_mytest_handler方法处理这个请求  
  73.     clcf->handler = ngx_http_mytest_handler;  
  74.   
  75.     return NGX_CONF_OK;  
  76. }  
  77.   
  78. //实际完成处理的回调函数    
  79. static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)  
  80. {  
  81.     //必须是GET或者HEAD方法,否则返回405 Not Allowed  
  82.     if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD)))  
  83.     {  
  84.         return NGX_HTTP_NOT_ALLOWED;  
  85.     }  
  86.   
  87.     //丢弃请求中的包体  
  88.     ngx_int_t rc = ngx_http_discard_request_body(r);  
  89.     if (rc != NGX_OK)  
  90.     {  
  91.         return rc;  
  92.     }  
  93.   
  94.     //设置返回的Content-Type。注意,ngx_str_t有一个很方便的初始化宏  
  95. //ngx_string,它可以把ngx_str_t的data和len成员都设置好  
  96.     ngx_str_t type = ngx_string("text/plain");  
  97.     //返回的包体内容  
  98.     ngx_str_t response = ngx_string("Hello World!");  
  99.     //设置返回状态码  
  100.     r->headers_out.status = NGX_HTTP_OK;  
  101.     //响应包是有包体内容的,所以需要设置Content-Length长度  
  102.     r->headers_out.content_length_n = response.len;  
  103.     //设置Content-Type  
  104.     r->headers_out.content_type = type;  
  105.   
  106.     //发送http头部  
  107.     rc = ngx_http_send_header(r);  
  108.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)  
  109.     {  
  110.         return rc;  
  111.     }  
  112.   
  113.     //构造ngx_buf_t结构准备发送包体  
  114.     ngx_buf_t                 *b;  
  115.     b = ngx_create_temp_buf(r->pool, response.len);  
  116.     if (b == NULL)  
  117.     {  
  118.         return NGX_HTTP_INTERNAL_SERVER_ERROR;  
  119.     }  
  120.     //将Hello World拷贝到ngx_buf_t指向的内存中  
  121.     ngx_memcpy(b->pos, response.data, response.len);  
  122.     //注意,一定要设置好last指针  
  123.     b->last = b->pos + response.len;  
  124.     //声明这是最后一块缓冲区  
  125.     b->last_buf = 1;  
  126.   
  127.     //构造发送时的ngx_chain_t结构体  
  128.     ngx_chain_t     out;  
  129.     //赋值ngx_buf_t  
  130.     out.buf = b;  
  131.     //设置next为NULL  
  132.     out.next = NULL;  
  133.   
  134.     //最后一步发送包体,http框架会调用ngx_http_finalize_request方法  
  135. //结束请求  
  136.     return ngx_http_output_filter(r, &out);  
  137. }  


三、修改配置文件 

        最后一步是修改nginx.conf配置,为http块中增加一个对应于mytest模块的location块:

[cpp]  view plain  copy
  1. location /test {  
  2.         mytest;  
  3. }  

如下图所示:


四、编译安装新模块

        重新编译Nginx源码使其包含新定义的模块(注需要把以前安装的删掉):


[cpp]  view plain  copy
  1. ./configure  --add-module=/home/nginx(这个路径是上面的conf和ngx_http_mytest_module.c文件所在的路径)  
  2. make  
  3. make install  

五、验证结果

        安装好Nginx之后,打开服务器:/usr/local/nginx/sbin/nginx 

        然后在浏览器输入:http://localhost/test即可看到Hello World字样。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值