$msec
The $msec variable contains the current time in milliseconds since the epoch. — NGINX Core (HTTP)
Description
The $msec variable is a built-in variable in NGINX that provides high-resolution time information. It returns the current system time in milliseconds since the Unix epoch (January 1, 1970). This value is set at the time a request is being processed and is typically used for logging, debugging, and performance measurement purposes. When a request comes in, NGINX retrieves the current time and converts it into milliseconds, which you can then use in various contexts such as access logs, custom headers, or for conditional processing based on timing. The precision of this variable makes it especially valuable when you need to measure response times or calculate delays between different stages of request processing. The value output by $msec is a decimal number representing the total milliseconds elapsed since the epoch. This variable is often used alongside other time-related variables such as $time_iso8601 for generating detailed logs that require both formatted date and millisecond precision. It is important to remember that the accuracy of the $msec variable is subject to the underlying operating system and hardware clock resolution.
Config Example
log_format custom_format '$remote_addr - [$time_local] "${request}" $status $body_bytes_sent "$http_referer" "$http_user_agent" $msec';
access_log /var/log/nginx/access.log custom_format;Subsystem
httpCacheable
NoContexts
http, server, location, ifEnsure the time on your server is synchronized (e.g., using NTP) to avoid discrepancies in logged times.
Be cautious when comparing $msec with non-millisecond precision timestamps; this can lead to confusion during debugging.
Using $msec in conjunction with request processing and timing mechanisms (e.g., measuring request duration) should be done carefully to prevent performance issues. In high-throughput situations, excessive logging could impact performance.