$protocol

The variable $protocol in NGINX Stream context returns the protocol being used by the connection, such as TCP or UDP. — NGINX Core (Stream)

$protocol NGINX Core (Stream)

Description

The $protocol variable in NGINX Stream is an automatically set variable that becomes available within the same scope as other stream directives, particularly during stream block execution. This variable indicates the protocol for the established connection, indicating whether it is TCP or UDP. It is primarily used to facilitate logging, access control, or conditional configuration based on the type of protocol being handled. This variable is set when a connection is established to the server, depending on the protocol specified in the server and upstream blocks. Typical values for this variable include 'tcp' and 'udp', which provide a clear distinction of what type of traffic is being processed. This is particularly useful in environments where both TCP and UDP protocols are handled by the same server process, allowing for differentiated configurations and logging behaviors based on the protocol.

Config Example

stream {
    server {
        listen 12345;
        proxy_pass backend;
        log_format custom_format '$remote_addr - $protocol';
        access_log /var/log/nginx/access.log custom_format;
    }
}

Subsystem

stream

Cacheable

Yes

Contexts

stream, server

Ensure that the stream block is properly defined; otherwise, the variable will not be set.

The $protocol variable is only available in stream contexts, not in http context or other contexts.