12

I am trying to inject the time of an nginx server into an HTTP header.

I am able to add to an HTTP header, like so:

proxy_set_header HELLO-WORLD 'something';

But now, I want to be able to inject the time into an HTTP header, something that looks like this:

proxy_set_header THE-TIME $time_var;

Or something like that.

Would that be possible?

Sal Rahman
  • 4,607
  • 2
  • 30
  • 43

2 Answers2

15

You can use variables from SSI modulе: $date_gmt and $date_local

proxy_set_header THE-TIME $date_gmt;

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

VBart
  • 14,714
  • 4
  • 45
  • 49
  • 2
    Hi, I want to know how to format the date here. I want it formatted as year-month like `2016-12` – Stoneboy Dec 22 '16 at 03:25
  • 2
    I was able to format datetime in an nginx response: `return 200 '\n';` But no idea how to use SSI for header value. The variable $date_gmt is provided by the nginx ssi module (it's a normal nginx variable, so it can be used in a header value), but apparently cannot be formatted using ` – Messa Mar 28 '18 at 15:40
  • @Messa, I solved this issue like this: `` `` `` In nginx config, you can get the variable through `$arg_date` – abdulmanov.ilmir Oct 14 '19 at 10:29
4

$time_iso8601 is both machine- & human-friendly.

Alex
  • 8,245
  • 8
  • 46
  • 55