Nginx之一:实现简单HTTP模块helloworld



查看原文:http://www.wyblog.cn/2017/04/27/nginx%e4%b9%8b%e4%b8%80%ef%bc%9a%e5%ae%9e%e7%8e%b0%e7%ae%80%e5%8d%95http%e6%a8%a1%e5%9d%97helloworld/本篇博文记录如何在Nginx里开发一个简单的HTTP模块,实现响应。

调用过程

修改conf/nginx.conf

在http{}中的server{}里添加 location /test模块,见下图:

生成一个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"
这个文件跟底下的文件放在同一个目录里。

ngx_http_mytest_module.c

代码:
#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, //类型,指定配置项可以出现的位置
        ngx_http_mytest, //配置项的回调函数  见下文
        NGX_HTTP_LOC_CONF_OFFSET, //在配置文件中的偏移量
        0,
        NULL
    },
    ngx_null_command
};
//模块上下文
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 = {
    //#define NGX_MODULE_V1 0,0,0,0,0,0,1 包含了ctx_index 与 index 分别为在一类模块中的序号及所有模块里的序号
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,  //ctx  http框架的要求
    ngx_http_mytest_commands,     //commands 处理nginx.conf中的配置项
    NGX_HTTP_MODULE,   //指定模块类型
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
};

//配置项对应的回调函数
static char * 
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_core_loc_conf_t *clcf; //clcf指向一个location内的配置块

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    //注:上边有一个 ngx_http_mytest_module 两者结构一样 

    clcf->handler = ngx_http_mytest_handler; //名称匹配后的回调函数

    return NGX_CONF_OK;
}

//实际完成处理的回调函数
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    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;
    }

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

    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;
    }

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

    ngx_chain_t out;
    out.buf = b;
    out.next = NULL;

    return ngx_http_output_filter(r, &out);
}

编译

配置好以上所有文件后,将config与ngx_http_mytest_module.c 这两个文件放在同一个文件夹里,比如~/nginx-1.2.0/test。 然后编译nginx带上模块地址:
./configure --add-module /home/**/nginx-1.2.0/test
然后make -B,再make install。

测试

启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
浏览器访问: 恩,成功了!  

查看原文:http://www.wyblog.cn/2017/04/27/nginx%e4%b9%8b%e4%b8%80%ef%bc%9a%e5%ae%9e%e7%8e%b0%e7%ae%80%e5%8d%95http%e6%a8%a1%e5%9d%97helloworld/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值