nginx 开发一个简单的 HTTP 模块

jopen 8年前

1. 下载 Nginx  

http://nginx.org/

2. 目录结构

$ tree -L 2  .  ├── mytest_module  │   ├── config  │   └── ngx_http_mytest_module.c  └── nginx  ...

3. config

# 在 configure 执行时使用  ngx_addon_name="ngx_http_mytest_module"  # HTTP 模块名称  HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"  # 新增模块源代码  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

4. ngx_http_mytest_module.c

/*   * mytest_module   */  #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);    /*   * --ngx_command_t:用于处理 nginx.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      },      ngx_null_command  };    /*   * --ngx_http_module_t 上下文结构体   */  static ngx_http_module_t ngx_http_mytest_module_ctx = {      NULL,      NULL,        NULL,      NULL,        NULL,      NULL,        NULL,      NULL  };    /*   * --ngx_module_t    *  configure 后,会被加入到 ngx_modules.c 中   */  ngx_module_t ngx_http_mytest_module = {      // ctx_index,index,spare0,spare1,spare2,spare3,version      NGX_MODULE_V1,      // 上下文结构体      &ngx_http_mytest_module_ctx,      // 用于处理 nginx.conf 中的配置项结构      ngx_http_mytest_commands,      // 模块类型,HTTP模块      NGX_HTTP_MODULE,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NGX_MODULE_V1_PADDING  };    // 处理 nginx.conf 配置项参数的方法  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配置项所在的配置块相匹配,调用该方法      clcf->handler = ngx_http_mytest_handler;        return NGX_CONF_OK;  }    // 如果请求的主机域名、URI与mytest配置项所在的配置块相匹配,调用该方法  static ngx_int_t   ngx_http_mytest_handler(ngx_http_request_t *r)  {      // 必须是 GET 获得 HEAD 方法      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;      }        // Content-Type      ngx_str_t type = ngx_string("text/plain");      // 包体内容      ngx_str_t response = ngx_string("Hello World!");      // 设置返回状态码      r->headers_out.status = NGX_HTTP_OK;      // 设置Content-Length      r->headers_out.content_length_n = response.len;      // 设置Content-Type      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;      b = ngx_create_temp_buf(r->pool, response.len);      if (b == NULL) {          return NGX_HTTP_INTERNAL_SERVER_ERROR;      }        // 将 Hello World 复制到 ngx_buf_t 指向的内存中      ngx_memcpy(b->pos, response.data, response.len);      b->last = b->pos + response.len;      b->last_buf = 1;      // 构造发送时的 ngx_chain_t 结构体      ngx_chain_t out;      out.buf = b;      out.next = NULL;        // 发送包体      return ngx_http_output_filter(r, &out);  }

5. 编译、运行

修改配置:

nginx$ vi conf/nginx.conf

加入如下:

# (添加)非守护进程运行  daemon off;  # (添加)master 自身处理请求   master_process on;       events {       .....   }     http {      ....      server {      # 修改 80 为 8080      listen 8080;      ....          # (添加) 到遇到 GET /test 请求时,使用 mytest 模块处理          location /test {              mytest;          }          ...      }      ...  }

configure:

nginx$ ./configure --prefix=/tmp/ngx_study/ --add-module=../mytest_module

在运行 configure 后,会在 objs/ngx_modules.c 文件的 ngx_modules 数组中加入 ngx_http_mytest_module 信息:

$ cat objs/ngx_modules.c | grep http_mytest_module  extern ngx_module_t  ngx_http_mytest_module;      &ngx_http_mytest_module,

编译:

nginx$ make  nginx$ make install

运行:

$ /tmp/ngx_study/sbin/nginx

在浏览器中输入:

localhost:8080/test

6. PS 猜想:

nginx 通过 ngx_modules.c 中的结构 ngx_module_t ngx_modules -> 

ngx_http_mytest_module.c 中的结构 ngx_module_t ngx_http_mytest_module -> 结构 ngx_command_t ngx_http_mytest_commands -> 函数ngx_http_mytest 设置 处理函数 ngx_http_mytest_handler。

参考:

《深入理解 Nginx ——模块开发与架构解析》

来自: http://my.oschina.net/lowkey2046/blog/553038