what's the proper way to get module specific configuration in init_process

Maxim Dounin mdounin at mdounin.ru
Wed Jun 22 17:51:07 UTC 2022


On Mon, Jun 20, 2022 at 04:05:53PM +0800, shanlei at asiainfo.com wrote:

> Hello, guys,
> Recently I have been working on an nginx c module, which have to 
> leverage on init_process routine.The logic is based on instruction 
> command which may be configured inside stream.server,  I have to do some 
> work in the init_process routine, for example, to schedule a timer.
> I did google a while ,and decide to using code snippet like below:
> ============================
> ngx_stream_conf_ctx_t conf_ctx = (ngx_stream_conf_ctx_t 
> *)ngx_get_conf(cycle->conf_ctx, ngx_stream_module);
> "module_spec_conf = conf_ctx->srv_conf["my_module".ctx_index];
> =============================
> However, the module_spec_conf  I got is the top_level ,that is created 
> when nginx parse the stream section, not the one created when nginx 
> parse the stream.server section, the later has the proper values. 
> Currently to got the later structure, I have to copy the configure value 
> back from child to parent in configuration merger function, which is 
> inconvenient and buggy when the stream includes multi servers.
> So I wonder what's the proper way to get module specific configuration 
> in the routine: init_process

For the purpose of the init_process callback, the most simple 
approach would be to use your module's main configuration.  For 
example, the embedded Perl module uses the init process callback 
to set correct PID in Perl interpreter: Perl interpreter is stored 
in the module's main configuration, and the init process callback 
uses the ngx_http_cycle_get_module_main_conf() macro to access it.  
See src/http/modules/perl/ngx_http_perl_module.c for details.

(Just in case, corresponding macro in the stream module is 

Maxim Dounin

More information about the nginx-devel mailing list