NGINX头部和身体过滤器模块 [英] NGINX Header and Body Filter Modules

查看:168
本文介绍了NGINX头部和身体过滤器模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个NGINX过滤器模块,可以读取/写入传入请求的cookie。如果一个特定的cookie没有被正确设置(即认证cookie),它将把出站标题的状态设置为相应的错误代码。这可以按照 Evan Miller的教程的指导进行。下一部分,我正在尝试工作(并没有到目前为止)是身体过滤器被调用,所以我可以在遇到错误响应时插入/替换正文响应文本。我再次遵循关于身体过滤器,我不能为了我的生活得到这个工作。这里是我的设置:

  static ngx_http_output_header_filter_pt ngx_http_next_header_filter; 
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
...
...


static ngx_http_module_t ngx_http_source_cookie_module_ctx = {
NULL,/ *预配置* /
ngx_http_source_cookie_init,/ *后配置* /

NULL,/ *创建主配置* /
NULL,/ * init主配置* /
$ b $ NULL,/ *创建服务器配置* /
NULL,/ *合并服务器配置* /

ngx_http_source_cookie_create_loc_conf,/ *创建位置配置* /
ngx_http_source_cookie_merge_loc_conf / *合并位置配置* /
};

ngx_module_t ngx_http_source_cookie_module = {
NGX_MODULE_V1,
& ngx_http_source_cookie_module_ctx,/ *模块上下文* /
ngx_http_source_cookie_commands,/ *模块指令* /
NGX_HTTP_MODULE, / *模块类型* /
NULL,/ * init master * /
NULL,/ * init模块* /
NULL,/ * init进程* /
NULL,/ * init线程* /
NULL,/ *退出线程* /
NULL,/ *退出进程* /
NULL,/ * exit master * /
NGX_MODULE_V1_PADDING
} ;
/ * -------------------------------------------- ------------------------------ * /

/ * -------- -------------------------------------------------- ---------------- * /
static ngx_int_t
ngx_http_source_cookie_header_filter(ngx_http_request_t * r)
{
//被调用
...
}

/ * ------------------------------ -------------------------------------------- * /

/ * -------------------------------------------- ------------------------------ * /
static ngx_int_t
ngx_http_body_filter(ngx_http_request_t * r,ngx_chain_t * in)
{
//这个永远不会被调用
...
}

/ * ----------- -------------------------------------------------- ------------- * /

/ * ------------------------- ------------------------------------------------- * /
static ngx_int_t
ngx_http_source_cookie_init(ngx_conf_t * cf)
{
//注册我的过滤器

ngx_http_next_header_filter = ngx_http_top_he ader_filter;
ngx_http_top_header_filter = ngx_http_source_cookie_header_filter;

ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_body_filter;

返回NGX_OK;



$ b $ p
$ b这是我的基本设置,据我所知,在所有的例子/教程我遇到过。我想知道是否有什么不同,我需要启用...像一个NGINX配置选项,NGINX ./configure编译选项等。

任何帮助是很大的我注意到Evan并没有修正 ngx_http_< module_name> _header_filter()中的http内容长度,



如果我不添加http内容长度( r-> headers_out.content_length_n ),插入的文本到请求的结尾将不会从nginx-1.2.7 stable输出。



你也可以看到页脚过滤器模块


I've been coding an NGINX filter module that can read/write cookies for incoming requests. If a particular cookie isn't set correctly (i.e. authentication cookie), it will set the outgoing header status to the appropriate error code. This works fine per the directions of Evan Miller's tutorial. The next part I'm trying to get working (and haven't thus far) is having the body filter be invoked so I can insert/replace body response text when error responses are encountered. I again followed Evan Miller's tutorial on body filters, and I cannot for the life of me get this working. Here's my setup:

static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
...
...


static ngx_http_module_t ngx_http_source_cookie_module_ctx = {
    NULL,                             /* preconfiguration */
    ngx_http_source_cookie_init,             /* postconfiguration */

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

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

    ngx_http_source_cookie_create_loc_conf,  /* create location configuration */
    ngx_http_source_cookie_merge_loc_conf    /* merge location configuration */
};

ngx_module_t ngx_http_source_cookie_module = {
    NGX_MODULE_V1,
    &ngx_http_source_cookie_module_ctx,      /* module context */
    ngx_http_source_cookie_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_int_t
ngx_http_source_cookie_header_filter(ngx_http_request_t *r)
{
   // this gets invoked
   ...
}

/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
   // this never get invoked
   ...
}

/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_source_cookie_init(ngx_conf_t *cf)
{
    // registering of my filters

    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_source_cookie_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_body_filter;

    return NGX_OK;
}

This is my basic setup, and as far as I can tell, it's spot on all the examples/tutorials I've come across. I'm wondering if there's something different altogether I need to enable... like a NGINX config option, NGINX ./configure compile option, etc.

Any help is greatly appreciated.

解决方案

I note that Evan doesnt fix http content length in ngx_http_<module_name>_header_filter().

If I dont add http content length(r->headers_out.content_length_n), the inserted text to the end of request will not be output from nginx-1.2.7 stable.

Also you can see footer filter module.

这篇关于NGINX头部和身体过滤器模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆