Forum: NGINX Accessing the location configuration of module 2 during post configuration processing of module 1 fo

95763e65fd1ff9f48963e8973d054db9?d=identicon&s=25 Rv Rv (Guest)
on 2014-06-09 20:09
(Received via mailing list)
How do we access the configuration of a an unrelated module in a given
module. This may be required for example to check if the directives
pertaining to module 2 were specified in location for a particular
server that has directives for module 1 in its configuration.

From what I understand, code similar to this can be used
/* Get http main configuration */
  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];

/* Get the list of servers */
  cscfp = cmcf->servers.elts;
/* Iterate through the list */
  for (s = 0; s < cmcf->servers.nelts; s++) {
 /* Problem : how to get the configuration of module 2*/
         cscfp[s]->ctx->loc_conf[module2.ctx_index];-------------> does
not yield the correct location struct of module 2

I did not find any documentation on how the configuration is stored
within nginx using these structs
typedef struct {
.............
/* server ctx */ ngx_http_conf_ctx_t        *ctx;............

} ngx_http_core_srv_conf_t;


typedef struct {
  void    **main_conf;
  void    **srv_conf;
  void    **loc_conf;
} ngx_http_conf_ctx_t;
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-06-10 16:53
(Received via mailing list)
Hello!

On Tue, Jun 10, 2014 at 02:09:13AM +0800, Rv Rv wrote:

> How do we access the configuration of a an unrelated module in a
> given module. This may be required for example to check if the
> directives pertaining to module 2 were specified in location for
> a particular server that has directives for module 1 in its
> configuration.

I don't think it's something you should do at postconfiguration -
location structure is complex and not easily accessible.  There
are location configuration merge callbacks where you are expected
to work with location configs and, in particular, can use
ngx_http_conf_get_module_loc_conf() macro to access a
configuration of other modules (note though, that order of modules
may be important in this case).

[...]

> I did not find any documentation on how the configuration is stored within nginx
using these structs

It's under src/, in C language.

I would rather say it's not a part of the API, and you'd better
avoid using it directly.

--
Maxim Dounin
http://nginx.org/
95763e65fd1ff9f48963e8973d054db9?d=identicon&s=25 Rv Rv (Guest)
on 2014-06-12 07:50
(Received via mailing list)
Hello Maxim
Thanks for your response. Here is a related query.
Say in module 1 I have a
typedef struct {
int flag;
  ngx_str somestring;
}module1;

flag gets initialized with the following code


  { ngx_string("module1_directive"),
   NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
   ngx_conf_set_flag_slot,
   NGX_HTTP_LOC_CONF_OFFSET,
   offsetof(module1,configured),
   NULL },

somestring gets iniitialized with a handler written in the module (i.e
not ngx_conf_set_flag_slot or any inbuilt handler).

In the post configuration, I see that flag is not properly set but
somestring is. Flag is properly set during request processing though.
Are the values set during processing of a directive in location struct
guaranteed to be set by the time post configuration is executed?
When is the time that one can check for the values set during
configuration. I need to test these values to ensure that they are sane
when nginx is executed with -t option

Thanks

Hello!

On Tue, Jun 10, 2014 at 02:09:13AM +0800, Rv Rv wrote:

> How do we access the configuration of a an unrelated module in a
> given module. This may be required for example to check if the
> directives pertaining to module 2 were specified in location for
> a particular server that has directives for module 1 in its
> configuration.

I don't think it's something you should do at postconfiguration -
location structure is complex and not easily accessible. There
are location configuration merge callbacks where you are expected
to work with location configs and, in particular, can use
ngx_http_conf_get_module_loc_conf() macro to access a
configuration of other modules (note though, that order of modules
may be important in this case).

[...]

> I did not find any documentation on how the configuration is stored within nginx
using these structs?

It's under src/, in C language.

I would rather say it's not a part of the API, and you'd better
avoid using it directly.

--
Maxim Dounin
http://nginx.org/



------------------------------



On Monday, 9 June 2014 11:39 PM, Rv Rv <rvrv7575@yahoo.com> wrote:



How do we access the configuration of a an unrelated module in a given
module. This may be required for example to check if the directives
pertaining to module 2 were specified in location for a particular
server that has directives for module 1 in its configuration.

From what I understand, code similar to this can be used
/* Get http main configuration */
  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];

/* Get the list of servers */
  cscfp = cmcf->servers.elts;
/* Iterate through the list */
  for (s = 0; s < cmcf->servers.nelts; s++) {
 /* Problem : how to get the configuration of module 2*/
         cscfp[s]->ctx->loc_conf[module2.ctx_index];-------------> does
not yield the correct location struct of module 2

I did not find any documentation on how the configuration is stored
within nginx using these structs
typedef struct {
.............
/* server ctx */ ngx_http_conf_ctx_t        *ctx;............

} ngx_http_core_srv_conf_t;


typedef struct {
  void    **main_conf;
  void    **srv_conf;
  void    **loc_conf;
} ngx_http_conf_ctx_t;
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-06-14 21:35
(Received via mailing list)
Hello!

On Thu, Jun 12, 2014 at 01:46:58PM +0800, Rv Rv wrote:

[...]

> In the post configuration, I see that flag is not properly set
> but somestring is. Flag is properly set during request
> processing though.
> Are the values set during processing of a directive in location
> struct guaranteed to be set by the time post configuration is
> executed?
> When is the time that one can check for the values set during
> configuration. I need to test these values to ensure that they
> are sane when nginx is executed with -t option

Again: there are lots of location configurations, and by trying to
access them at postconfiguration callback you are likely checking
a wrong one.  Note that even a simple config with a single
location in a single server{} block, like this:

     http { server { location { ... } } }

has 3 location configurations for each http module.

As previously suggested, you should consider using merge callbacks
to validate configuration instead.

--
Maxim Dounin
http://nginx.org/
95763e65fd1ff9f48963e8973d054db9?d=identicon&s=25 Rv Rv (Guest)
on 2014-06-16 09:02
(Received via mailing list)
Hello Maxim
Thanks for the response.
>>As previously suggested, you should consider using merge callbacks>>to validate
configuration instead.

The requirement is *not* to validate the configuration. The requirement
is to find the final value set by one of the directives once the
configuration has been parsed. e..g lets say we have a directive
my_set_flag that sets a value to 0 or 1.

So if in configuration we have
location \ {
my_set_flag 0;
---
my_set_flag 1;
---
my_set_flag 0;
}

then the merge callback will be called thrice for each invocation of the
directive. Let's assume the logic is to set a variable with whatever the
value of the directive was. So once parsing completes, the value of the
variable should be 0.

I can get this value during request processing. However, I cannot get
this value *after* the parsing of the configuration has completed. What
is the nginx recommended way to get this value. As noted in earlier
post, I am not seeing the correct values in post configuration - and so
perhaps that is not the right way.

Thanks for your continued inputs


Hello!

On Thu, Jun 12, 2014 at 01:46:58PM +0800, Rv Rv wrote:

[...]

> In the post configuration, I see that flag is not properly set
> but somestring is. Flag is properly set during request
> processing though.
> Are the values set during processing of a directive in location
> struct guaranteed to be set by the time post configuration is
> executed?
> When is the time that one can check for the values set during
> configuration. I need to test these values to ensure that they
> are sane when nginx is executed with -t option

Again: there are lots of location configurations, and by trying to
access them at postconfiguration callback you are likely checking
a wrong one. Note that even a simple config with a single
location in a single server{} block, like this:

http { server { location { ... } } }

has 3 location configurations for each http module.

As previously suggested, you should consider using merge callbacks
to validate configuration instead.

--
Maxim Dounin
http://nginx.org/



On Thursday, 12 June 2014 11:16 AM, Rv Rv <rvrv7575@yahoo.com> wrote:



Hello Maxim
Thanks for your response. Here is a related query.
Say in module 1 I have a
typedef struct {
int flag;
ngx_str somestring;
}module1;

flag gets initialized with the following code


  { ngx_string("module1_directive"),
   NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
   ngx_conf_set_flag_slot,
   NGX_HTTP_LOC_CONF_OFFSET,
   offsetof(module1,configured),
   NULL },

somestring gets iniitialized with a handler written in the module (i.e
not ngx_conf_set_flag_slot or any inbuilt handler).

In the post configuration, I see that flag is not properly set but
somestring is. Flag is properly set during request processing though.
Are the values set during processing of a directive in location struct
guaranteed to be set by the time post configuration is executed?
When is the time that one can check for the values set during
configuration. I need to test these values to ensure that they are sane
when nginx is executed with -t option

Thanks

Hello!

On Tue, Jun 10, 2014 at 02:09:13AM +0800, Rv Rv wrote:

> How do we access the configuration of a an unrelated module in a
> given module. This may be required for example to check if the
> directives pertaining to module 2 were specified in location for
> a particular server that has directives for module 1 in its
> configuration.

I don't think it's something you should do at postconfiguration -
location structure is complex and not easily accessible. There
are location configuration merge callbacks where you are expected
to work with location configs and, in particular, can use
ngx_http_conf_get_module_loc_conf() macro to access a
configuration of other modules (note though, that order of modules
may be important in this case).

[...]

> I did not find any documentation on how the configuration is stored within nginx
using these structs?

It's under src/, in C language.

I would rather say it's not a part of the API, and you'd better
avoid using it directly.

--
Maxim Dounin
http://nginx.org/



------------------------------



On Monday, 9 June 2014 11:39 PM, Rv Rv <rvrv7575@yahoo.com> wrote:



How do we access the configuration of a an unrelated module in a given
module. This may be required for example to check if the directives
pertaining to module 2 were specified in location for a particular
server that has directives for module 1 in its configuration.

From what I understand, code similar to this can be used
/* Get http main configuration */
  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];

/* Get the list of servers */
  cscfp = cmcf->servers.elts;
/* Iterate through the list */
  for (s = 0; s < cmcf->servers.nelts; s++) {
 /* Problem : how to get the configuration of module 2*/
         cscfp[s]->ctx->loc_conf[module2.ctx_index];-------------> does
not yield the correct location struct of module 2

I did not find any documentation on how the configuration is stored
within nginx using these structs
typedef struct {
.............
/* server ctx */ ngx_http_conf_ctx_t        *ctx;............

} ngx_http_core_srv_conf_t;


typedef struct {
  void    **main_conf;
  void    **srv_conf;
  void    **loc_conf;
} ngx_http_conf_ctx_t;
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-06-16 16:15
(Received via mailing list)
Hello!

On Mon, Jun 16, 2014 at 02:58:50PM +0800, Rv Rv wrote:

> Hello Maxim
> Thanks for the response.
> >>As previously suggested, you should consider using merge callbacks>>to
validate configuration instead.
>

> ---
> my_set_flag 0;
> }

Such a configuration is invalid due to duplicate "my_set_flag"
directive, and will be rejected during configuration parsing.

> then the merge callback will be called thrice for each
> invocation of the directive. Let's assume the logic is to set a
> variable with whatever the value of the directive was. So once
> parsing completes, the value of the variable should be 0.

You are misunderstanding what merge callback is, what it does and
how it's called.

> I can get this value during request processing. However, I
> cannot get this value *after* the parsing of the configuration
> has completed. What is the nginx recommended way to get this
> value. As noted in earlier post, I am not seeing the correct
> values in post configuration - and so perhaps that is not the
> right way.

Again: accessing a location configuration from postconfiguration
callback isn't trivial and not supported.  You should use merge
callback instead, or use main or server configuration instead,
not location.

Before proceeding any further, I would recommend you to spend some
time digging into nginx configuration basics.  In particular, Evan
Miller's guide may be helpful, see links here:

http://nginx.org/en/links.html

--
Maxim Dounin
http://nginx.org/
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.