フィルターを使用して処理を行うカスタム nginx モジュールを作成しています。フィルタの設定は次のようになります。
static ngx_int_t ngx_http_mymodule_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_mymodule_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_mymodule_body_filter;
return NGX_OK;
}
このフィルターでいくつかのロジックを処理していbody
ますが、特定の条件で、クライアントのブラウザーで localhost へのリダイレクトを実行したいと考えています。フィルター本体のコードは次のようになります。
static ngx_int_t
ngx_http_mymodule_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ctx = ngx_http_get_module_ctx(r, ngx_http_mymodule_module);
if (ctx == NULL) {
return ngx_http_next_body_filter(r, in);
}
bool success = __my_custom_logic(r);
if (success == false) {
ngx_http_set_ctx(r, NULL, ngx_http_mymodule_module);
// Do the redirect here! How?
// Setting the Location header and changing r->headers_out.status to 302
// just returns an empty HTTP 200 to the client instead...
}
ngx_http_set_ctx(r, NULL, ngx_http_mymodule_module);
return ngx_http_send_special(r, NGX_HTTP_LAST);
}
私が試したこと:
- Location ヘッダーと headers_out.status を 302 に設定しますが、クライアントに空の応答を返します
- headers_out.location を設定しますが、これはまだ空の応答を返します
- ngx_http_send_header(r) を呼び出しますが、これは NGX_ERROR 応答を返します
クライアントへのリダイレクトで応答するにはどうすればよいですか?