$session_time
The $session_time variable returns the duration of the current stream session in seconds. — NGINX Core (Stream)
Description
The $session_time variable in NGINX is used to measure the duration of the current stream session, specifically within the Stream module. This variable is set when the NGINX server establishes a connection for a stream and continues to be updated until the connection is closed or completed. It represents the total time in seconds that the session has been active, making it useful for monitoring and logging purposes, as well as configuring limits on sessions based on their duration. Typically, $session_time will start at zero when the connection is initiated and will increase as time progresses, reflecting the ongoing duration of the session. This value can be particularly beneficial in setups where session timeouts need to be enforced or when analyzing session performance and usage patterns in a streaming application. It's important to note that this variable is specific to the Stream module and is not available in the HTTP or other modules. As such, it is primarily utilized in contexts where TCP/UDP-based streaming is involved, rather than standard web serving contexts.
Config Example
stream {
server {
listen 12345;
log_format custom_format '$remote_addr - $session_time';
access_log /var/log/nginx/stream_access.log custom_format;
}
}Subsystem
streamCacheable
NoContexts
stream, server, locationEnsure that the Stream module is enabled in your NGINX configuration; otherwise, the variable will not be accessible.
The variable $session_time is only valid during stream processing and will not work with HTTP contexts.
Be cautious when using $session_time in conditionals as they are evaluated at different times during the request processing. Ensure it aligns with the actual session duration needed.