NGINX modules act as either handlers, filters or load-balancers
Components
Config Structs
Modules define up to three configuration structs, each for the main, server and location contexts of the nginx.conf file. The calling conventions for these are ngx_http_(main|srv|loc)_conf_t.
For example, if we were to write a module targeting the server context we would instead use ngx_http_X_srv_conf_t.
The C struct above shows a few NGINX data types that serve as wrappers to the common primitive types of the C language.
Directives
Module directives populate the elements in the configuration structs and are called directly from the appropriate context of the configuration file. The structure is an array of commands. The exact definition ngx_command_t can be found core/ngx_conf_file.h.
This structure is where we would configure whether the directives take any arguments or have restricted access to specific contexts.
static ngx_command_t ngx_http_hello_world_commands[] = {
{
ngx_string("hello_world"), /* directive */
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes no arguments*/
ngx_http_hello_world_commands_set_method, /* configuration setup function */
NGX_RS_HTTP_LOC_CONF_OFFSET,
0, /* No offset when storing the module configuration on the struct. */
NULL
},
...
ngx_null_command // termination
};
A module's definition struct glues together the context and directives in one place. Additionally, we can also configure process-level functionality at either the life or death of the thread/process.