set_formatted_local_time
指令 `set_formatted_local_time` 将一个变量设置为根据指定格式字符串格式化的当前本地时间。
NGINX Set-Misc module
·
httpserverlocationif in serverif in location
语法set_formatted_local_time name format_string;
默认值none
上下文http, server, location, if in server, if in location
参数2
说明
set_formatted_local_time 指令对于在 NGINX 配置中直接生成格式化时间戳非常有用。它允许您将一个变量设置为当前本地时间,按照所提供的格式字符串使用标准库的格式化语法(类似 strftime)进行格式化。对于记录日志、缓存或其他与时间相关的配置,且时间戳的精确格式很重要的情况,这尤其有用。
在使用此指令时,您可以提供两个参数:要设置的变量名和格式字符串。格式字符串决定当前本地时间的格式,允许灵活指定日期和时间的表示方式。此功能可以增强 NGINX 配置,使日志或响应头更加信息化,确保时间戳易于阅读并且对调试或监控有用。
配置示例
set_formatted_local_time $current_time "%Y-%m-%d %H:%M:%S";
⚠
确保格式字符串根据 strftime 的语法正确指定。
⚠
注意时区;该指令使用服务器的本地时间设置。