Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1485751
  • 博文数量: 228
  • 博客积分: 1698
  • 博客等级: 上尉
  • 技术积分: 3241
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-24 21:49
个人简介

Linux

文章分类

全部博文(228)

文章存档

2017年(1)

2016年(43)

2015年(102)

2014年(44)

2013年(5)

2012年(30)

2011年(3)

分类: LINUX

2015-10-19 17:12:19

Nginx可以很方便的增加第三方模块,下面以Hello Word为例,简单介绍一下具体步骤。

1. 把模块源代码全部放在同一目录下,比如当前的目录为:/data1/xirui.hw/nginx/nginx-hello-world
2. nginx-hello-world下,编写config文件,config文件是一可执行脚本,开发一个最简单的HTTP模块,至少需要定义以下变量:
    ngx_add_addon_name:nginx configure执行时使用,记录当前模块名称
    HTTP_MODULES:HTTP模块名称,多个模块名称用空格分开,增加新模块时用“$HTTP_MODULES ngx_http_xx_module”的方式,以免覆盖旧模块
    NGX_ADDON_SRCS:指定新增模块源码,多个源码文件用空格分开,可以用$ngx_addon_dir变量 <其为configure时指定的--add-module=PATH参数>
    具体如下:
    
点击(此处)折叠或打开
  1. ngx_addon_name="ngx_http_hello_module"
  2. HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"
  3. NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello.c"
3. nginx configure执行时,加入参数--add-module=PATH(新增模块源代码以及config文件存放目录)

点击(此处)折叠或打开

  1. ./configure --prefix=/data1/xirui.hw/nginx/nginx_install --add-module=/data1/xirui.hw/nginx/nginx-hello-world
4. 修改nginx.conf,在http->server配置块中,增加location /test { hello; }配置项

点击(此处)折叠或打开

  1. http {
  2.     ..........
  3.   
  4.     server {
  5.         listen 8080;
  6.         server_name localhost;
  7.   
  8.         location /test {
  9.             hello;
  10.         }
  11.      }
  12.   
  13.     location / {
  14.         root html;
  15.         index index.html index.htm;
  16.     }
  17.     ........
5. 源码编写,具体如下:
点击(此处)折叠或打开
  1. #include <ngx_config.h>
  2. #include <ngx_core.h>
  3. #include <ngx_http.h>

  4. static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r);
  5. static char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

  6. static ngx_command_t ngx_http_hello_commands[] = {
  7.         {
  8.                 ngx_string("hello"),
  9.                 NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
  10.                 ngx_http_hello,
  11.                 NGX_HTTP_LOC_CONF_OFFSET,
  12.                 0,
  13.                 NULL
  14.         },
  15.         ngx_null_command
  16. };

  17. static ngx_http_module_t ngx_http_hello_module_ctx = {
  18.         NULL,
  19.         NULL,
  20.         NULL,
  21.         NULL,
  22.         NULL,
  23.         NULL,
  24.         NULL,
  25.         NULL
  26. };

  27. ngx_module_t ngx_http_hello_module = {
  28.         NGX_MODULE_V1,
  29.         &ngx_http_hello_module_ctx,
  30.         ngx_http_hello_commands,
  31.         NGX_HTTP_MODULE,
  32.         NULL,
  33.         NULL,
  34.         NULL,
  35.         NULL,
  36.         NULL,
  37.         NULL,
  38.         NULL,
  39.         NGX_MODULE_V1_PADDING
  40. };

  41. static char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  42. {
  43.         ngx_http_core_loc_conf_t *clcf;

  44.         clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  45.         clcf->handler = ngx_http_hello_handler;

  46.         return NGX_CONF_OK;
  47. }

  48. static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r)
  49. {
  50.         if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
  51.                 return NGX_HTTP_NOT_ALLOWED;
  52.         }

  53.         ngx_int_t rc = ngx_http_discard_request_body(r);
  54.         if (rc != NGX_OK) {
  55.                 return rc;
  56.         }

  57.         ngx_str_t type = ngx_string("text/plain");
  58.         ngx_str_t response = ngx_string("Hello World!");
  59.         r->headers_out.status = NGX_HTTP_OK;
  60.         r->headers_out.content_length_n = response.len;
  61.         r->headers_out.content_type = type;

  62.         rc = ngx_http_send_header(r);
  63.         if(rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  64.                 return rc;
  65.         }

  66.         ngx_buf_t *b = ngx_create_temp_buf(r->pool,response.len);
  67.         if (b == NULL) {
  68.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  69.         }

  70.         ngx_memcpy(b->pos, response.data, response.len);
  71.         b->last = b->pos + response.len;
  72.         b->last_buf = 1;

  73.         ngx_chain_t out;
  74.         out.buf = b;
  75.         out.next = NULL;

  76.         return ngx_http_output_filter(r, &out);
  77. }
6. 编译安装
点击(此处)折叠或打开
  1. ./configure --prefix=/data1/xirui.hw/nginx/nginx_install --add-module=/data1/xirui.hw/nginx/nginx-hello-world
  2. make
  3. make install
7. 启动Nginx,浏览器输入,可以看到输出
Hello World!

阅读(1203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~