Welcome! Log In Create A New Profile

Advanced

Preaccess handler being called twice

Posted by rp2 
rp2
Preaccess handler being called twice
August 09, 2012 09:08AM
Hi:

I have a simple module. When I attach a handler to the preaccess phase it ends up being called twice. Any idea why and how i can fix it (code below)?

// Output:

$> curl http://localhost:8090^C
...
$> tail -f error.log |grep AAA
2012/08/09 18:35:45 [debug] 3977#0: *5 AAAAAA: xx handler 9
2012/08/09 18:35:45 [debug] 3977#0: *5 AAAAAA: xx handler 10


// Code

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

static char *ngx_http_play(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_play_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_play_fn(ngx_http_request_t *r);

static ngx_command_t ngx_http_play_commands[] = {

{ ngx_string("play"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_play,
0,
0,
NULL },

ngx_null_command
};



static ngx_http_module_t ngx_http_play_module_ctx = {
NULL, /* preconfiguration */
ngx_http_play_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_play_module = {
NGX_MODULE_V1,
&ngx_http_play_module_ctx, /* module context */
ngx_http_play_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 int i=0;

static ngx_int_t ngx_http_play_fn(ngx_http_request_t *r)
{
i++;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "AAAAAA: xx handler %d", i);
return NGX_OK;
}

static char *ngx_http_play(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0, "AAAAAA: conf setup ", i);
return NGX_CONF_OK;
}

static ngx_int_t ngx_http_play_init(ngx_conf_t *cf){
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;

cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}


*h = ngx_http_play_fn;
return NGX_OK;
}
Re: Preaccess handler being called twice
May 13, 2016 03:40AM
Hi,
I got same problem in my sample.
I found that redirect raise this issue.

you can load your page by http://127.0.0.1/index.html
not http://127.0.0.1/

nginx will redirect the request to index.html to find default page.

i used ngx_http_request_t uri_changes to avoid run my logic twice.

if (r->uri_changes == 11){
... YOUR CODE
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 320
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready