How nginx processes a TCP/UDP session

A TCP/UDP session from a client is processed in successive steps called phases:

The first phase after accepting a client connection. The ngx_stream_realip_module module is invoked at this phase.
Preliminary check for access. The ngx_stream_limit_conn_module and ngx_stream_set_module modules are invoked at this phase.
Client access limitation before actual data processing. The ngx_stream_access_module module is invoked at this phase.
TLS/SSL termination. The ngx_stream_ssl_module module is invoked at this phase.
Reading initial bytes of data into the preread buffer to allow modules such as ngx_stream_ssl_preread_module analyze the data before its processing.
Mandatory phase where data is actually processed, usually proxied to upstream servers, or a specified value is returned to a client.
The final phase where the result of a client session processing is recorded. The ngx_stream_log_module module is invoked at this phase.