Main module API¶
This page summarises core types and structures that are central to NGINX itself and widely referenced from modules.
The content here is adapted from the main.rst Module API documentation in
the historical NGINX Wiki.
Basic scalar types¶
These are convenience typedefs used throughout the NGINX core and modules:
ngx_pid_t: integer type used to store process IDsngx_cycle_t: event cycle object representing the current runtime statengx_uint_t: alias forunsigned intngx_int_t: alias forintngx_flag_t: boolean flag type (typically0or1)ngx_msec_t: millisecond count, alias forngx_uint_tngx_file_t: describes an open file descriptor and its metadatangx_peer_connection_t: represents an upstream peer connectionsize_t,off_t,time_t,in_port_t,va_list,ssize_t,uintptr_t,uint32_t: standard C types used with conventional meanings
ngx_module_t¶
ngx_module_t is the central structure used to define a module.
Key members (simplified):
ctx: module‑specific context pointer, usually pointing to:ngx_core_module_tfor core modulesngx_http_module_tfor HTTP modulesngx_mail_module_tfor mail modulescommands: pointer to anngx_command_tarray defining configuration directives for the moduletype: module type, set using one of:NGX_CORE_MODULENGX_HTTP_MODULENGX_EVENT_MODULENGX_MAIL_MODULENGX_STREAM_MODULE- Lifecycle hooks:
init_master(ngx_log_t *log)init_module(ngx_cycle_t *cycle)init_process(ngx_cycle_t *cycle)init_thread(ngx_cycle_t *cycle)exit_thread(ngx_cycle_t *cycle)exit_process(ngx_cycle_t *cycle)exit_master(ngx_cycle_t *cycle)
The NGX_MODULE_V1 and NGX_MODULE_V1_PADDING macros are used for the header
and footer of the structure respectively.
Example (from the original docs):
ngx_module_t ngx_http_my_module = {
NGX_MODULE_V1,
&ngx_http_my_module_ctx, /* module context */
ngx_http_my_module_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
};
ngx_core_module_t¶
Defines the core context for a module:
name: module name (ngx_str_t)create_conf(cycle): allocates and initialises configuration datainit_conf(cycle): finalises configuration based on parsed directives
ngx_http_module_t¶
Defines the HTTP module context, used by all HTTP modules:
preconfiguration(cf): called before parsing HTTP configurationpostconfiguration(cf): called after configuration is parsedcreate_main_conf(cf)/init_main_conf(cf, conf)create_srv_conf(cf)/merge_srv_conf(cf, prev, conf)create_loc_conf(cf)/merge_loc_conf(cf, prev, conf)
These callbacks control allocation and merging of configuration structures at
the http, server, and location levels.
ngx_mail_module_t¶
Defines the mail module context:
protocol: pointer to anngx_mail_protocol_tstructurecreate_main_conf(cf)/init_main_conf(cf, conf)create_srv_conf(cf)/merge_srv_conf(cf, prev, conf)
ngx_connection_t¶
Represents a single connection (for example a client TCP connection).
For module authors, the most commonly referenced member is:
log: pointer to anngx_log_tused for logging in the context of the connection.