Nigix加入Hello World模块

详细资料:<深入理解Nginx>

 

 

1.编写第三方模块

建立文件夹hello,里面有两个文件:

ngx_http_mytest_module.c

  1 #include <ngx_config.h>
  2 #include <ngx_core.h>
  3 #include <ngx_http.h>
  4 
  5 static char *
  6 ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf);
  7 static ngx_int_t 
  8 ngx_http_mytest_handler(ngx_http_request_t *r);
  9 
 10 
 11 static ngx_command_t ngx_http_mytest_commands[]={
 12     {
 13         //配置项名称
 14         ngx_string("mytest"),
 15         //配置项类型(可出现的位置,参数的个数)
 16         NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
 17         //出现了name中指定的配置项后,将会调用该方法处理配置项的参数
 18         ngx_http_mytest,
 19         NGX_HTTP_LOC_CONF_OFFSET,
 20         0,
 21         NULL
 22     },
 23     ngx_null_command
 24 };
 25 
 26 static char *
 27 ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf)
 28 {
 29     //找到mytest配置项所属的配置块
 30     ngx_http_core_loc_conf_t *clcf;
 31     clcf=ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
 32     /*
 33         HTTP框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时
 34         如果请求的主机域名、URI与mytest配置项所在的配置块相匹配,
 35         就将调用我们事先的ngx_http_mytest_handler方法处理这个请求
 36     */
 37     clcf->handler=ngx_http_mytest_handler;
 38     return NGX_CONF_OK;
 39 }
 40 
 41 static ngx_http_module_t ngx_http_mytest_module_ctx={
 42     NULL,
 43     NULL,
 44     NULL,
 45     NULL,
 46     NULL,
 47     NULL,
 48     NULL,
 49     NULL
 50 };
 51 
 52 ngx_module_t ngx_http_mytest_module={
 53     NGX_MODULE_V1,
 54     //指向ngx_http_module_t结构体
 55     &ngx_http_mytest_module_ctx,
 56     //用来处理nginx.conf中的配置项
 57     ngx_http_mytest_commands,
 58     //表示该模块的类型
 59     NGX_HTTP_MODULE,
 60     NULL,
 61     NULL,
 62     NULL,
 63     NULL,
 64     NULL,
 65     NULL,
 66     NULL,
 67     NGX_MODULE_V1_PADDING
 68 };
 69 
 70 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
 71 {
 72     //判断请求方法是否为GET或者HEAD,否则返回405 Not Allowed
 73     if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))){
 74         return NGX_HTTP_NOT_ALLOWED;
 75     }
 76     
 77     //丢弃请求中的包体
 78     ngx_int_t rc=ngx_http_discard_request_body(r);
 79     if(rc!=NGX_OK){
 80         return rc;
 81     }
 82     
 83     //设置相应头
 84     ngx_str_t type=ngx_string("text/plain");
 85     ngx_str_t response=ngx_string("Hello World!");
 86     r->headers_out.status=NGX_HTTP_OK;
 87     r->headers_out.content_length_n=response.len;
 88     r->headers_out.content_type=type;
 89     
 90     //发送HTTP头部
 91     rc=ngx_http_send_header(r);
 92     if(rc==NGX_ERROR||rc>NGX_OK||r->header_only){
 93         return rc;
 94     }
 95     
 96     //构造ngx_buf_t结构体准备发送包体
 97     ngx_buf_t *b;
 98     b=ngx_create_temp_buf(r->pool,response.len);
 99     if(b==NULL){
100         return NGX_HTTP_INTERNAL_SERVER_ERROR;
101     }
102     ngx_memcpy(b->pos,response.data,response.len);
103     b->last=b->pos+response.len;
104     b->last_buf=1;
105     
106     //构造发送时的ngx_chain_t结构体
107     ngx_chain_t out;
108     out.buf=b;
109     out.next=NULL;
110     return ngx_http_output_filter(r,&out);
111 }
View Code

config

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"
View Code

 

2.编译第三方模块(nginx源码目录下)

在未安装nginx的情况下安装第三方模块

# ./configure --prefix=/usr/local/nginx \
  --add-module=/第三方模块目录

在已安装nginx情况下安装第三方模块

# ./configure --prefix=/usr/local/nginx \
  --add-module=/第三方模块目录
# make
# /usr/local/nginx/sbin/nginx -s stop
# cp objs/nginx /usr/local/nginx/sbin/nginx
# /usr/local/nginx/sbin/nginx

 

3.使用第三方模块

使用一个模块需要根据这个模块定义的配置指令来做。比如我们这个简单的hello handler module的时候就很简单。

在nginx.conf文件中在http里面的默认的server里面加入如下配置

location /test {
    mytest;
}

 

4.使用telnet查看结果

 

posted @ 2015-10-12 15:42  Runnyu  阅读(727)  评论(0编辑  收藏  举报