设为首页 收藏本站
查看: 1338|回复: 0

[经验分享] nginx模块开发之“敲录”

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-3-27 09:28:28 | 显示全部楼层 |阅读模式
nginx支持高并发,占用少量系统资源的webserver。如果听说某人会nginx模块开发,我们暗赞-------高大上。其实不然,只要你树立目标,暗下苦工,明天的你就是“高富帅”。我在2012年下半年就树立目标,研读nginx代码,工具就是GOOG,中间断断续续。到13年下半年,稍稍有点成绩,可以照葫芦画瓢,搞点小东西,原理层面的东西,并不是很理解。后来朋友推荐,看到了陶辉老师写的“深入理解Nginx”,发现很是值得我们细细品味。下面是我敲录的一个模块代码:

第一步:查看文件
[iyunv@centos6 nginx-http-top-filter-1.1.1]# ll
total 12
-rw-r--r-- 1 root root  189 Feb 25 17:29 config
-rw-r--r-- 1 root root 5094 Mar 15 20:13 ngx_http_top_filter_module.c

第二步:编辑config配置文件
[iyunv@centos6 nginx-http-top-filter-1.1.1]# vim config   #编辑config配置文件

ngx_addon_name=ngx_http_top_filter_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_top_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_top_filter_module.c"

第三步:编写模块
[iyunv@centos6 nginx-http-top-filter-1.1.1]# vim ngx_http_top_filter_module.c
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>


typedef struct {
    ngx_flag_t enable;
} ngx_http_top_conf_t;

typedef struct {
    ngx_int_t add_prefix;

} ngx_http_top_ctx_t;

static void *ngx_http_top_create_conf(ngx_conf_t *cf);
static char *ngx_http_top_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_top_filter_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_topfilter_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_topfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in);

static ngx_str_t filter_prefix = ngx_string("[my filter prefix]");

/*----step------3--------*/
static ngx_command_t  ngx_http_top_filter_commands[] = {
{ ngx_string("add_prefix"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_top_conf_t,enable),
      NULL },

      ngx_null_command
};

/*----step------2--------*/
static ngx_http_module_t  ngx_http_top_filter_module_ctx = {
    NULL,                               /* proconfiguration */
    ngx_http_top_filter_init,        /* postconfiguration */

    NULL,                               /* create main configuration */
    NULL,                               /* init main configuration */

    NULL,                               /* create server configuration */
    NULL,                               /* merge server configuration */

    ngx_http_top_create_conf,    /* create location configuration */
    ngx_http_top_merge_conf      /* merge location configuration */
};
/*-----step-------1--------------------------*/
ngx_module_t  ngx_http_top_filter_module = {
    NGX_MODULE_V1,
    &ngx_http_top_filter_module_ctx, /* module context */
    ngx_http_top_filter_commands,    /* module directives */
    NGX_HTTP_MODULE,                    /* module type */
    NULL,                               /* init master */
    NULL,                               /* init module */
    NULL,                               /* init process */
    NULL,                               /* init thread */
    NULL,                               /* exit thread */
    NULL,                               /* exit process */
    NULL,                               /* exit master */
    NGX_MODULE_V1_PADDING
};

static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt   ngx_http_next_body_filter;

/*----step----4----------------*/
static ngx_int_t
ngx_http_topfilter_header_filter(ngx_http_request_t *r)
{
    ngx_http_top_ctx_t *ctx;
    ngx_http_top_conf_t *conf;

    if(r->headers_out.status != NGX_HTTP_OK){
return ngx_http_next_header_filter(r);
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_top_filter_module);

    if(ctx){
        return ngx_http_next_header_filter(r);
    }

    conf = ngx_http_get_module_loc_conf(r, ngx_http_top_filter_module);

    if(conf->enable ==0){
        return ngx_http_next_header_filter(r);
    }

    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_top_ctx_t));
    if(ctx == NULL){
        return NGX_ERROR;
    }

    ctx->add_prefix = 0;
    ngx_http_set_ctx(r, ctx, ngx_http_top_filter_module);
    /*注意测试文件后缀必须是.txt,如果是html,需要将红色部分修改,原因自己想想,会明白的,当时我测试的时候,也想了一下。*/
    if(r->headers_out.content_type.len >= sizeof("text/plain") -1 && ngx_strncasecmp(r->headers_out.content_type.data, (u_
char *) "text/plain", sizeof("text/plain") -1) ==0){
        ctx->add_prefix = 1;

        if(r->headers_out.content_length_n > 0)
        {
            r->headers_out.content_length_n += filter_prefix.len;
        }

    }
    return ngx_http_next_header_filter(r);

}

static void *ngx_http_top_create_conf(ngx_conf_t *cf)
{
    ngx_http_top_conf_t  *mycf;

    mycf = (ngx_http_top_conf_t *)ngx_pcalloc(cf->pool, sizeof(ngx_http_top_conf_t));
    if (mycf == NULL) {
        return NULL;
    }

    mycf->enable= NGX_CONF_UNSET;

    return mycf;
}

static char *
ngx_http_top_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_http_top_conf_t  *prev = (ngx_http_top_conf_t *)parent;
    ngx_http_top_conf_t  *conf = (ngx_http_top_conf_t *)child;

    ngx_conf_merge_value(conf->enable, prev->enable, 0);

    return NGX_CONF_OK;
}


    static ngx_int_t
ngx_http_top_filter_init(ngx_conf_t *cf)
{

    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_topfilter_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_topfilter_body_filter;


    return NGX_OK;
}

static ngx_int_t ngx_http_topfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_http_top_ctx_t *ctx;
    ctx = ngx_http_get_module_ctx(r, ngx_http_top_filter_module);

    if(ctx == NULL){
        return ngx_http_next_body_filter(r, in);
    }

    ctx->add_prefix = 2;
    ngx_buf_t *b = ngx_create_temp_buf(r->pool, filter_prefix.len);


    b->pos = filter_prefix.data;
    b->last = b->pos + filter_prefix.len;
    b->start = b->pos;

    ngx_chain_t *c1 = ngx_alloc_chain_link(r->pool);
    if (c1 == NULL )
    {
        return NGX_ERROR;
    }

    c1->buf = b;
    c1->next = in;

    return ngx_http_next_body_filter(r, c1);

}

       文档中的数字标号,是我自己搞的标注,方便阅读用的。线上环l境中用的一个nginx修改版,最近在想怎样通过模块的方式实现,但是还没搞好,改天搞好以后,再贴到这里。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-16282-1-1.html 上篇帖子: mac nginx 403错误,修改目录权限的问题 下篇帖子: Linux上Nginx如何添加多个虚拟主机配置 开发
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表