2012-06-19 52 views
5

我一直在編寫一個NGINX過濾器模塊,它可以讀取/寫入傳入請求的cookie。如果一個特定的cookie沒有被正確設置(即認證cookie),它會將出局標題狀態設置爲相應的錯誤代碼。這工作正常的方向Evan Miller's tutorial。接下來的部分我正在嘗試工作(而且迄今爲止還沒有)是要調用body filter,以便在遇到錯誤響應時插入/替換主體響應文本。我再次關注身體過濾器上的Evan Miller's tutorial,我無法爲我的生活得到這個工作。這是我的設置:NGINX頭和身體過濾器模塊

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

這是我的基本設置,據我所知,它是所有我遇到的示例/教程的位置。我想知道是否有什麼不同,我需要啓用...像一個NGINX配置選項,NGINX ./configure編譯選項等。

任何幫助,非常感謝。

+0

什麼是您正在使用的NGINX版本?你使用了什麼./configure參數?我有一個類似的問題試圖運行穩定版本的身體過濾器模塊,然後我嘗試使用dev版本(1.3.5),它的工作原理(不知道爲什麼)。 – Alex

回答

0

我注意到Evan並沒有修復http內容長度ngx_http_<module_name>_header_filter()

如果我不添加http內容長度(r->headers_out.content_length_n),則插入的文本到請求結束時不會從nginx-1.2.7 stable輸出。

你也可以看到footer filter module