фильтр дублирует ответы

o1egus nginx-forum на nginx.us
Вт Июн 1 12:03:20 MSD 2010


Привет.

Написал простой фильтр для nginx. 
nginx фронтенд(localhost:8000). apache бэкенд(localhost:80).

Код фильтра.
[code]
#include "pg.h"

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static ngx_int_t ngx_http_pg_filter_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_pg_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_pg_body_filter(ngx_http_request_t *r, ngx_chain_t *in);

static ngx_http_module_t  ngx_http_pg_module_ctx = {
    NULL,                          /* preconfiguration */
    ngx_http_pg_filter_init,       /* postconfiguration */

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

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

    NULL,  						   /* create location configuration */
    NULL						   /* merge location configuration */
};

ngx_module_t  ngx_http_pg_module = {
    NGX_MODULE_V1,
    &ngx_http_pg_module_ctx, 	   /* module context */
    NULL,		   /* 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;

static ngx_int_t ngx_http_pg_header_filter(ngx_http_request_t *r)
{

	return ngx_http_next_header_filter(r);
}

static ngx_int_t ngx_http_pg_body_filter(ngx_http_request_t *r, ngx_chain_t *in) {

	if(in == NULL || r->header_only)
		return ngx_http_next_body_filter(r, in);
	ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,"buf_size:%d", ngx_buf_size(in->buf));
	ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,"status:%d", r->headers_out.status);
	return ngx_http_next_body_filter(r, in);
}

static ngx_int_t ngx_http_pg_filter_init(ngx_conf_t *cf) {
    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_pg_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_pg_body_filter;

    return NGX_OK;
}
[/code]

Файл nginx.conf
[code]
daemon off;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  0;

    server {

        listen       8000;
        server_name  localhost;
	
	location / {
		proxy_pass http://localhost:80;
	}
    }
}
[/code]

В браузере шлю запрос http://localhost:8000/1.html.

1.html выглядит так:
[code]
HELLO
[/code]

Получаю в error.log
[code]
#нормальный ответ
2010/06/01 14:54:15 [error] 11576#0: *1 buf_size:5 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /1.html HTTP/1.1", upstream: "http://127.0.0.1:80/1.html", host: "localhost:8000"
2010/06/01 14:54:15 [error] 11576#0: *1 status:200 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /1.html HTTP/1.1", upstream: "http://127.0.0.1:80/1.html", host: "localhost:8000"

#повторный ответ с пустым буфером
2010/06/01 14:54:15 [error] 11576#0: *1 [b]buf_size:0[/b] while sending to client, client: 127.0.0.1, server: localhost, request: "GET /1.html HTTP/1.1", upstream: "http://127.0.0.1:80/1.html", host: "localhost:8000"
2010/06/01 14:54:15 [error] 11576#0: *1 status:200 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /1.html HTTP/1.1", upstream: "http://127.0.0.1:80/1.html", host: "localhost:8000"


#нормальный ответ
2010/06/01 14:54:15 [error] 11576#0: *3 buf_size:283 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:80/favicon.ico", host: "localhost:8000"
2010/06/01 14:54:15 [error] 11576#0: *3 status:404 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:80/favicon.ico", host: "localhost:8000"

#повторный ответ с пустым буфером
2010/06/01 14:54:15 [error] 11576#0: *3 [b]buf_size:0[/b] while sending to client, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:80/favicon.ico", host: "localhost:8000"
2010/06/01 14:54:15 [error] 11576#0: *3 status:404 while sending to client, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:80/favicon.ico", host: "localhost:8000"
[/code]

Откуда берется повторный ответ с пустым буфером?

Posted at Nginx Forum: http://forum.nginx.org/read.php?21,93023,93023#msg-93023




Подробная информация о списке рассылки nginx-ru