【问题标题】:Is there a way to get the current time in nginx?有没有办法在 nginx 中获取当前时间?
【发布时间】:2012-05-28 18:50:31
【问题描述】:

我正在尝试将 nginx 服务器的时间注入 HTTP 标头。

我可以添加到 HTTP 标头,如下所示:

proxy_set_header HELLO-WORLD 'something';

但现在,我希望能够将时间注入 HTTP 标头,如下所示:

proxy_set_header THE-TIME $time_var;

或者类似的东西。

这可能吗?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您可以使用 SSI 模块中的变量:$date_gmt 和 $date_local

    proxy_set_header THE-TIME $date_gmt;
    

    http://nginx.org/en/docs/http/ngx_http_ssi_module.html#variables

    【讨论】:

    • 嗨,我想知道如何在这里格式化日期。我希望它格式化为像 2016-12 这样的年月
    • 我能够在 nginx 响应中格式化日期时间:return 200 '<!--#config timefmt="%Y-%m-%dT%H:%M:%SZ" --><!--#echo var="DATE_GMT" -->\n'; 但不知道如何使用 SSI 作为标头值。变量 $date_gmt 是由 nginx ssi 模块提供的(它是一个普通的 nginx 变量,所以它可以用在 header 值中),但显然不能使用<!--#config timefmt=... 格式化。
    • @Messa,我这样解决了这个问题:<!--# config timefmt="%Y-%m-%d" --><!--# set var="date" value="$date_local" --><!--# include virtual="/ssi.php?date=$date&and-other-parameters-here" -->在nginx config中,你可以通过$arg_date获取变量
    【解决方案2】:

    $time_iso8601 对机器和人类都友好。

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多