0

Consider a simple NGINX module:

typedef struct {
    ngx_array_t *claims;
} my_conf_t

static ngx_command_t my_commands[] = {
    { ngx_string("my_claims"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
        ngx_conf_set_str_array_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(my_conf_t, claims),
        NULL },

    ngx_null_command
};

static ngx_http_module_t my_module_ctx = {
    NULL,
    my_module_init,
    NULL,
    NULL,
    NULL,
    NULL,
    my_module_create_loc_conf,
    my_module_merge_loc_conf
};


ngx_module_t my_module = {
    NGX_MODULE_V1,
    &my_module_ctx,
    my_commands,
    NGX_HTTP_MODULE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
};

Without even making use of claims elsewhere in the module, this throws a segmentation fault during nginx -t when my new config option is used:

location / {
    my_claims test;
    return 200 OK;
}

That config throws a segmentation fault while commenting/removing the location allows nginx -t to succeed.

I looked examples for NGINX Mail SSL module (here and here) and I don't see what I'm missing.

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • What did `my_module_create_loc_conf` looked like, shouldn't `ngx_array_create` be called ? – dvhh Jul 22 '23 at 18:59

1 Answers1

0

The simple answer is that it segfaults because your module is incomplete. You need to register the configuration directives, etc.

ngx_module_t  ngx_http_my_module = {
    NGX_MODULE_V1,
    &my_module_ctx,  /* module context */
    my_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
};

And that is not enough still. It is best to check some open-source NGINX modules and use them as a boilerplate.

Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
  • Thank you. I do already have this in place (neglected to include it, sorry). I'm adding a string array command to an existing module which is already working (so the "registration" is already in place). I've simply added my new `ngx_array_t *claims` to the config struct, and then added the command. Any other changes I've made (e.g. new logic) is commented out. – Josh M. Mar 20 '23 at 19:03