init module callback called twice

Maxim Dounin mdounin at mdounin.ru
Thu Apr 8 13:10:37 UTC 2021


Hello!

On Thu, Apr 08, 2021 at 05:54:42AM -0400, xdrew wrote:

> Hello,
> 
> I'm developing a little custom module for nginx, and I need to execute some
> user code once my module is loaded. I do this by attaching to the hook in
> ngx_module_t structure:
> 
> ngx_module_t  ngx_http_hello_world_module = {
> ...
>     NULL,                                  /* init master */
>     init_module,                         /* init module */
>     NULL,                                  /* init process */
> ...
> }
> 
> static ngx_int_t init_module(ngx_cycle_t *cycle) {
> ngx_log_stderr (0, "Initializing module") ; }
> 
> Surprisingly my callback is called twice. First time it follows log message
> 
> nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
> nginx: Initializing module
> 
> and then 
> 
> nginx: configuration file /etc/nginx/nginx.conf test is successful
> nginx: Initializing module
> 
> Is there a way to recognize that I'm called in some different context (e.g.
> some value from ngx_cycle_t structure)?
> Or may be I'm doing something completely wrong?

What you observe is perfectly expected: the module initialization 
callback is called once per configuration parsing, and your output 
seems to be from running a startup script which does something 
like "nginx -t; nginx", which starts nginx twice: once to test the 
configuration, and again to actually start nginx.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list