I have HTTP API, that provides some historic values using URL that contains UNIX timestamp as selector. Example: http://apiserver.org/api/1664269800/value
The timestamp value must be one of 10minutes granularity, eg: 10:10:00, 10:10:10 transformed to timestamp
I need to implement "latest" functionality,
I have simple nginx configuration, the wanted functionality is that access to "/proxyredir" will pass to http://apiserver.org/api/1664269800/value/, where timestamp number is dynamically computed.
location /proxyredir/ {
proxy_pass http://apiserver.org/api/1664269800/value/;
}
And I need some magic to compute timestamp value dynamically, get actual datetime, truncate to 10min granularity and perform proxypass.
I studied nginx perl module https://nginx.org/en/docs/http/ngx_http_perl_module.html?_ga=2.25112314.176064600.1664266760-2134269433.1664266760 but without success.
Please any ideas how to solve this problem?
CodePudding user response:
Problem solved using nginx perl module. The trick is that set_perl directive must be defined in configuration toplevel "http" but this variable is reevaluated on every request if used in "location contex"
The configuration looks like:
http {
...
perl_set $tstamptrunc '
sub {
my $r = shift;
# UNIX TIMESTAMP trucated to 10minutes
$epoch_10m_trunc = int(time()/600)*600;
return "$epoch_10m_trunc";
}
';
...
location ~ /api/latest/(. ) {
resolver 8.8.8.8;
proxy_pass: https://remote.org/api/$tstamptrunc/$1;
}
}
The resolver definition is necessary if proxy_pass definition is dynamically defined.